/* Done By LI XIAO DONG (student NO: u070925u)
National University of Singapore
Project: Development of Vision System for Unmanned Ground Mecanum Vehicle
This program is dedicated to capture real time optical flow from two cameras with
intersect angle 180 or 120 degree. It includes Optical flow capturing, calculation
debug etc. Program for communication through serial port is also included.
It employ OpenCV 2.1 labrary and VC 2010 compiler.*/
// 2COPTICALFLOW.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//Function Prototypes
HANDLE openPort(int);
int DONE=0;
DWORD len,dwBytesRead=0;
static const double pi = 3.14159265358979323846;
#define NUM_FEATURES 100
int done=0;
//char port[16]="com", pnum[16]="Error";
//char port2[32];
static double square(int a)
{
return a * a;
}
/* This is just an inline that allocates images. I did this to reduce clutter in the
* actual computer vision algorithmic code. Basically it allocates the requested image
* unless that image is already non-NULL. It always leaves a non-NULL image as-is even
* if that image's size, depth, and/or channels are different than the request.
*/
static void allocateOnDemand( IplImage **img, CvSize size, int depth, int channels)
{
if ( *img != NULL ) return;
*img = cvCreateImage( size, depth, channels );
if ( *img == NULL )
{
//fprintf(stderr, "Error: Couldn't allocate image. Out of memory?\n");
exit(-1);
}
}
HANDLE openPort(int portnum)
{
//char port[11]="\\\\.\\com",
char port[32]="COM", pnum[]="ERROR";
itoa(portnum,pnum,10);
strcat(port,pnum);
HANDLE serial;
//serial=CreateFile(port2,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
//serial=CreateFile(port,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
//serial=CreateFile((LPCWSTR)port,GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
serial=CreateFile(TEXT("\\\\.\\com1"),(GENERIC_READ|GENERIC_WRITE),0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
//serial=CreateFile((LPCTSTR)3,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if(serial==INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){
DWORD dw = GetLastError();
printf("Error, %s Not Found\n\a", port);
done=1;
return 0;
}
printf("Com Error\n\a");
done=1;
return 0;
}/**/
DCB dcbSerialParams={0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if(!GetCommState(serial, &dcbSerialParams)){
//printf("Com State Error\n\a");
done=1;
return 0;
}
dcbSerialParams.BaudRate=CBR_9600;//CBR_baudrate
dcbSerialParams.ByteSize=8;
dcbSerialParams.Parity=NOPARITY;//NOPARITY, ODDPARITY, EVENPARITY
dcbSerialParams.StopBits=ONESTOPBIT;//ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS
dcbSerialParams.fRtsControl = RTS_CONTROL_ENABLE; // enable the control of RTS line
dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE; // enable the control of DTR line
if(!SetCommState(serial, &dcbSerialParams)){
printf("Serial Protocol Error\n\a");
done=1;
return 0;
}
EscapeCommFunction(serial,SETRTS);//set RTS pin
Sleep(100);
EscapeCommFunction(serial,CLRRTS);//clear RTS pin
EscapeCommFunction(serial,SETDTR);//set RTS pin
Sleep(100);
EscapeCommFunction(serial,CLRDTR);//clear RTS pin
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=1;
timeouts.ReadTotalTimeoutConstant=1;
timeouts.ReadTotalTimeoutMultiplier=5;
timeouts.WriteTotalTimeoutConstant=1;
timeouts.WriteTotalTimeoutMultiplier=5;
if(!SetCommTimeouts(serial,&timeouts)){
printf("Timeout Setting Error\n\a");
done=1;
return 0;
}
return serial;
}
int main(void)
{
FILE *sendto;
// array to be sent
float send[6];
float Header=911.0;
float Byte= 24.0;
// array to be sent
/*int send[6];
int Header=911;
int Byte= 24;
*/
/*short send[7];
short HeaderH=0x4944;//18756
short HeaderL=0x6459;//25689
short Byte=0x0508;//1288;//hi byte is 5 low bye is 8
*/
//CvCapture *input_video
CvCapture *input_video1;
CvCapture *input_video3;
//CvSize frame_size;
CvSize frame_size1;
CvSize frame_size3;
//long number_of_frames;
long number_of_frames1;
long number_of_frames3;
//long current_frame = 0;
long current_frame1 = 0;
long current_frame3 = 0;
/*static IplImage *frame = NULL, *frame1 = NULL, *frame1_1C = NULL;
static IplImage *frame2_1C = NULL, *eig_image = NULL, *temp_image = NULL, *pyramid1 = NULL, *pyramid2 = NULL;*/
static IplImage *frame0 = NULL, *frame1 = NULL, *frame1_1C = NULL;
static IplImage *frame2_1C = NULL, *eig_image0 = NULL, *temp_image0 = NULL, *pyramid1 = NULL, *pyramid2 = NULL;
static IplImage *frame9 = NULL, *frame3 = NULL, *frame3_1C = NULL;
static IplImage *frame4_1C = NULL, *eig_image9 = NULL, *temp_image9 = NULL, *pyramid3 = NULL, *pyramid4 = NULL;
//int number_of_features;
int number_of_features1;
int number_of_features3;
//int line_thickness;
int line_thickness1;
int line_thickness3;
int key_pressed;
//int i;
int i;
int j;
//double angle; double hypotenuse;
double angle1; double hypotenuse1;
double angle3; double hypotenuse3;
//CvPoint p,q;
CvPoint p,q;
CvPoint n,m;
//char optical_flow_found_feature[NUM_FEATURES];
char optical_flow_found_feature1[NUM_FEATURES];
char optical_flow_found_feature3[NUM_FEATURES];
//float optical_flow_feature_error[NUM_FEATURES];
float optical_flow_feature_error1[NUM_FEATURES];
float optical_flow_feature_error3[NUM_FEATURES];
/*CvPoint2D32f frame1_features[NUM_FEATURES];
CvPoint2D32f frame2_features[NUM_FEATURES];*/
CvPoint2D32f frame1_features[NUM_FEATURES];
CvPoint2D32f frame2_features[NUM_FEATURES];
CvPoint2D32f frame3_features[NUM_FEATURES];
CvPoint2D32f frame4_features[NUM_FEATURES];
//CvSize optical_flow_window;
CvSize optical_flow_window1;
CvSize optical_flow_window3;
//CvScalar line_color;
CvScalar line_color1;
CvScalar line_color3;
//CvTermCriteria optical_flow_termination_criteria;
CvTermCriteria optical_flow_termination_criteria1;
CvTermCriteria optical_flow_termination_criteria3;
/* Create an object that decodes the input video stream. */
//input_video = cvCaptureFromFile("C:\input.avi");
input_video1 = cvCaptureFromCAM(0);
input_video3 = cvCaptureFromCAM(1);
if (input_video1 == NULL || input_video3 == NULL )
//if (input_video3 == NULL)
{
/* Either the video didn't exist OR it uses a codec OpenCV
* doesn't support.
*/
//fprintf(stderr, "Error: Can't open video.\n");
return -1;
}
/* This is a hack. If we don't call this first then getting capture
* properties (below) won't work right. This is an OpenCV bug. We
* ignore the return value here. But it's actually a video frame.
*/
cvQueryFrame( input_video1 );/* Read the video's frame size out of the AVI. */
cvQueryFrame( input_video3 );/* Read the video's frame size out of the AVI. */
frame_size1.height = (int) cvGetCaptureProperty( input_video1, CV_CAP_PROP_FRAME_HEIGHT );
frame_size3.height = (int) cvGetCaptureProperty( input_video3, CV_CAP_PROP_FRAME_HEIGHT );
frame_size1.width = (int) cvGetCaptureProperty( input_video1, CV_CAP_PROP_FRAME_WIDTH );
frame_size3.width = (int) cvGetCaptureProperty( input_video3, CV_CAP_PROP_FRAME_WIDTH );
/* Determine the number of frames in the AVI. */
/* Go to the end of the AVI (ie: the fraction is "1") */
cvSetCaptureProperty( input_video1, CV_CAP_PROP_POS_AVI_RATIO, 1. );
cvSetCaptureProperty( input_video3, CV_CA
2-camera-optical-flow-NUS.rar_2Camera_VC 2010 serial port_两个摄像头_
版权申诉
12 浏览量
2022-09-24
03:11:39
上传
评论
收藏 7KB RAR 举报
![avatar](https://profile-avatar.csdnimg.cn/5df8bff20ad645abb899a1a8333a748d_weixin_42651281.jpg!1)
小波思基
- 粉丝: 74
- 资源: 1万+
最新资源
- 基于 python 操作大数据的各种组件
- cifar-10-python和cifar-100-python
- 基于 C#实现神经网络算法和遗传算法作为AI的中国象棋程序
- Windows server 2016.2019 .NET Frameword 3.5安装包
- chilkat 9.5.0.98 系列软件 global unlock code
- 基于 python神经网络实现人类颜值评分
- 使用android实现计算器功能
- 305建筑结构水电欧式6套(14.5x20.2)\施工图1-11卫生间大样031026.dwg
- 305建筑结构水电欧式6套(14.5x20.2)\施工图1-09楼梯大样031026.dwg
- 基于PCL、C++编写的格网边缘点检测程序与代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
评论0