互动投影代码逻辑(可单独定制)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | #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; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步