#include <windows.h> #include <iostream> using namespace std; #include "camerads.h" #include "strsafe.h" #include <highgui.h> #include <stdlib.h> #include <math.h> using namespace cv; int pickering = 0; int cvimg_show_source = 0; int cvimg_show_diff = 0; int index = 0; int sensi = 100; CvPoint a; CvPoint b; CvPoint c; CvPoint d; CvPoint pbegin; CvPoint pend; int ReadConfig(LPTSTR key) { int m_nTest; LPTSTR strConfigFile; strConfigFile = _T("C://config.ini"); m_nTest = GetPrivateProfileInt(_T("Custom_Config"),key, -1, strConfigFile); //如果没有从配置文件中找到,则默认为0 return m_nTest; } void SaveConfig( LPCTSTR lpKeyName, INT Value) { LPTSTR strConfigFile; TCHAR ValBuf[16]; strConfigFile = _T("C://config.ini"); StringCbPrintfW( ValBuf,sizeof(ValBuf) ,TEXT( "%i" ), Value); WritePrivateProfileString(_T("Custom_Config"),lpKeyName, ValBuf , strConfigFile); } void SaveConfig(LPTSTR key,LPTSTR val) { LPTSTR strConfigFile; strConfigFile = _T("C://config.ini"); WritePrivateProfileString(_T("Custom_Config"),key, val , strConfigFile); } void save_config_int(LPTSTR key,int val){ SaveConfig((LPTSTR)(key),val); } int read_config(LPCTSTR key){ BEGIN: int re = ReadConfig((LPTSTR)(key)); if(re == -1){ save_config_int((LPTSTR)key,0); goto BEGIN; } return re; } void default_conf( int& index, CvPoint& a, CvPoint& b, CvPoint& c, CvPoint& d ){ index = 0; a.x=0; a.y=0; b.x=0; b.y=0; c.x=0; c.y=0; d.x=0; d.y=0; } void key_events(){ if(GetAsyncKeyState(VK_ESCAPE)) { //esc pickering = 0; } if(GetAsyncKeyState( VK_F1 )) { //f1 cvimg_show_source = 1; } if(GetAsyncKeyState(VK_F2)) { //f2 cvimg_show_source = -1; } if(GetAsyncKeyState(VK_F3)) { //f3 cvimg_show_diff = cvimg_show_diff?0:1; } } //鼠标点击 void mouse_click(int x,int y){ SetCursorPos(x,y);//移动到某点坐标 mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);//点下左键 mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);//松开左键 } void callback(int i,int j){ float bai_x = 0,bai_y = 0; if(i - a.x >0 && i< b.x){ if(b.x - a.x > 0){ bai_x = (float)(i - a.x)/(b.x - a.x); } } if(j - a.y >0 && j< d.y){ if(d.y - a.y > 0){ bai_y = (float)(j - a.y)/(d.y - a.y); } } int x,y; if(pend.x - pbegin.x > 0){ if(pend.y - pbegin.y > 0){ x = (int)(bai_x*(pend.x - pbegin.x) + pbegin.x); y = (int)(bai_y*(pend.y - pbegin.y) + pbegin.y); //MessageBox(NULL,TEXT("鼠标点击"),TEXT("warning"),0); //鼠标点击 mouse_click(x,y); } } } void onTrackerSlid(Mat &inputimage1, Mat &inputimage2, Mat &outputimage, int pos) { uchar *data1 = NULL; uchar *data2 = NULL; uchar *data3 = NULL; //uchar *data = NULL; int i, j; outputimage = inputimage1.clone(); int rowNumber = outputimage.rows; int colNumber = outputimage.cols*outputimage.channels(); int step = outputimage.step/sizeof(uchar); data1 = (uchar*)inputimage1.data; data2 = (uchar*)inputimage2.data; data3 = (uchar*)outputimage.data; for(i = 0; i < rowNumber; i++) { //data = (uchar*)outputimage.ptr<uchar>(i); //获取第i行的首地址 for(j = 0; j < colNumber; j++) { if(fabs((float)(data2[i*step + j] - data1[i*step + j])) > pos){ data3[i*step + j] = 255; callback(i,j); break; } else data3[i*step + j] = 0; } } } void frame_diff(IplImage* p1,IplImage* p2){ Mat matp1 = Mat(p1); Mat matp2 = Mat(p2); Mat dist = Mat(p1); //sensi = 100; onTrackerSlid(matp1,matp2,dist,sensi); if(cvimg_show_diff){ imshow("dstimage", dist); }else{ cvDestroyWindow("dstimage"); } //IplImage img = IplImage(fmat); //namedWindow("foreground", CV_WINDOW_AUTOSIZE); //imshow("foreground", fmat); } int init(){ //仅仅获取摄像头数目 int m_iCamCount = CCameraDS::CameraCount(); if(m_iCamCount == 0) { return (-1); } CCameraDS m_CamDS; IplImage *pFrame0 = NULL; IplImage *pFrame_1 = NULL; //获取所有摄像头的名称 if(m_iCamCount <= index){ MessageBox(NULL,TEXT("not found camera"),TEXT("warning"),0); } m_CamDS.OpenCamera(index, false, 320, 240); pickering = 1; int is_first = 1; while(1) { //获取键盘输入 key_events(); //获取一帧 pFrame0 = m_CamDS.QueryFrame(); if(is_first){ pFrame_1 = pFrame0; is_first = 0; } frame_diff(pFrame0,pFrame_1); //cvCopy(pFrame0,pFrame_1); pFrame_1 = cvCloneImage(pFrame0); //显示 if(cvimg_show_source == 1){ cvNamedWindow("video"); cvShowImage("video", pFrame0); } if(cvimg_show_source == -1){ cvDestroyWindow("video"); } if (cvWaitKey(20) == 'q') { break; } if(!pickering){ break; } Sleep(5); } m_CamDS.CloseCamera(); //可不调用此函数,CCameraDS析构时会自动关闭摄像头 cvDestroyWindow("video"); return 0; } void conf(){ a.x = read_config(_T("a_x")); a.y = read_config(_T("a_y")); b.x = read_config(_T("b_x")); b.y = read_config(_T("b_y")); c.x = read_config(_T("c_x")); c.y = read_config(_T("c_y")); d.x = read_config(_T("d_x")); d.y = read_config(_T("d_y")); pbegin.x = read_config(_T("begin_x")); pbegin.y = read_config(_T("begin_y")); pend.x = read_config(_T("end_x")); pend.y = read_config(_T("end_y")); index = read_config(_T("index")); sensi = read_config(_T("sensi")); } int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow){ conf(); init(); return 0; }