#include #include #include #include #include #include #define BINARY_THRESHOLD 240 LONG Get_ScreenWidth() { RECT rect; GetWindowRect(GetDesktopWindow(),&rect); return rect.right ¨C rect.left; } LONG Get_ScreenHight() { RECT rect; GetWindowRect(GetDesktopWindow(),&rect); return rect.bottom ¨C rect.top; } void Get_ScreenWidth(DWORD dx,DWORD dy) { DWORD event=0; event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); } void Left_MouseDown(DWORD dx,DWORD dy) { DWORD event=0; event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); } void Left_MouseUp(DWORD dx,DWORD dy) { DWORD event=0; event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); } void Right_MouseDown(DWORD dx,DWORD dy) { DWORD event=0; event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); } void Right_MouseUp(DWORD dx,DWORD dy) { DWORD event=0; event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); } void Left_SingleClick(DWORD dx,DWORD dy) { DWORD event=0; event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); } void Right_SingleClick(DWORD dx,DWORD dy) { DWORD event=0; event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); } void Left_DoubleClick(DWORD dx,DWORD dy) { Left_SingleClick(dx,dy); Left_SingleClick(dx,dy); } void Right_DoubleClick(DWORD dx,DWORD dy) { Right_SingleClick(dx,dy); Right_SingleClick(dx,dy); } void Mouse_Move(DWORD dx,DWORD dy) { DWORD event=0; event = MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE; mouse_event(event, dx*65535/Get_ScreenWidth(), dy*65535/Get_ScreenHight(), 0, 0); } IplImage* filterNoise(IplImage *img) { static IplImage* pyr = NULL; if(!pyr) { pyr = cvCreateImage( cvSize(img->width/2, img->height/2), 8, 1 ); } cvSmooth(img, img, CV_GAUSSIAN, 3, 0, 0); cvPyrDown(img, pyr, CV_GAUSSIAN_5x5); cvPyrUp(pyr, img, CV_GAUSSIAN_5x5); cvErode(img, img, 0, 1); cvDilate(img, img, 0, 1); return img; } IplImage* convertToBinary(IplImage *in, IplImage *out) { cvCvtColor(in, out, CV_BGR2GRAY); cvThreshold(out, out, BINARY_THRESHOLD, 255, CV_THRESH_BINARY); return out; } void findBlobs(IplImage *bin_img, IplImage* debug_img) { static CvMemStorage *storage = cvCreateMemStorage(0); CvSeq * contour; CvScalar color; DWORD event=0; cvFindContours( bin_img, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE); for(; contour; contour = contour->h_next) { CvRect rect = cvBoundingRect(contour, 1); CvPoint pt1 = cvPoint(rect.x, rect.y), pt2 = cvPoint(rect.x+rect.width, rect.y+rect.height); if(debug_img) { cvRectangle(debug_img, pt1, pt2, CV_RGB(255,0,0),2); cvDrawContours(debug_img, contour, CV_RGB(255,0,0), CV_RGB(255, 0, 0), 0, 2, 8); color = CV_RGB(0,255,0); cvCircle(debug_img, cvPoint((pt1.x+pt2.x)/2, (pt1.y+pt2.y)/2), 10, color, 3); Mouse_Move((pt1.x+pt2.x)/2, (pt1.y+pt2.y)/2); } } } int main(int argc, char** argv) { CvCapture* capture; IplImage *img, *bin_img = NULL; int key; capture = cvCaptureFromCAM(0); cvNamedWindow(¡°mainWin¡±, CV_WINDOW_AUTOSIZE); cvMoveWindow(¡°mainWin¡±, 600, 100); while(cvGrabFrame(capture)) { img=cvRetrieveFrame(capture); if(!bin_img) { bin_img=cvCreateImage( cvGetSize(img), IPL_DEPTH_8U, 1 ); } cvFlip(img, NULL, 0); convertToBinary(img, bin_img); filterNoise(bin_img); findBlobs(bin_img, img); cvFlip(img, NULL, 0); cvShowImage(¡°mainWin¡±, img); key=cvWaitKey(1); if(key == 27 || key == ¡®q¡¯) { break; } if(key == ¡® ¡®) { cvWaitKey(0); } } DWORD dx = 0,dy = 0; while(1) { if(dx >1024) { dx = 0; } if(dy >1024) { dy = 0; } Mouse_Move(dx++,dy++); Left_SingleClick(dx,dy); key=cvWaitKey(20); if(key == 27 || key == ¡®q¡¯) { break; } } cvReleaseCapture(&capture); return 0; }