Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "StdAfx.h"
- #include "cv.h"
- #include "highgui.h"
- #include <stdio.h>
- #include <Windows.h>
- #include <iostream>
- using namespace std;
- using namespace cv;
- static int posX = 0;
- static int posY = 0;
- int lastX = posX;
- int lastY = posY;
- uchar* ptr = NULL;
- CvScalar lorange = cvScalar(20,100,100);
- CvScalar hirange = cvScalar(30,255,255);
- bool do_track = false;
- bool use_hsv = true;
- int clicked_x = -1;
- int clicked_y = -1;
- void mousehandler( int event, int x, int y, int flags, void* param )
- {
- clicked_x = -1;
- if( event == CV_EVENT_LBUTTONDOWN )
- {
- clicked_x = x;
- clicked_y = y;
- printf("clicked %d %d\n", x,y);
- }
- }
- IplImage* GetThresholdedImage(IplImage* rec)
- {
- IplImage* GetThresholdedImage(IplImage* rec);
- IplImage* recHSV = cvCreateImage(cvGetSize(rec), 8, 3);
- cvCvtColor(rec, recHSV, CV_BGR2HSV);
- IplImage* recThreshed = cvCreateImage(cvGetSize(rec), 8, 1);
- cvInRangeS(recHSV,cvScalar(20,100,100),cvScalar(30,255,255), recThreshed);
- cvReleaseImage(&recHSV);
- return recThreshed;
- }
- IplImage* GetThresholdedImageRGB(IplImage* rec)
- {
- IplImage* recThreshed = cvCreateImage(cvGetSize(rec), 8, 1);
- cvInRangeS(rec,cvScalar(20,100,100),cvScalar(30,255,255), recThreshed);
- return recThreshed;
- }
- void GetPrimaryScreenSizeInPixels( SIZE& s)
- {
- ZeroMemory( &s, sizeof(SIZE) );
- s.cx = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
- s.cy = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
- }
- int clamp(int v)
- {
- if (v < 0 )
- return 0;
- if (v > 255)
- return 255;
- return v;
- }
- CvMemStorage* storage = cvCreateMemStorage(0);
- CvSeq* cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , storage);
- int main()
- {
- CvCapture* zaznam = cvCaptureFromCAM( CV_CAP_ANY); //zaznamenavanie z kamery
- if ( !zaznam )
- {
- fprintf( stderr, "Chyba! zaznam je bez hodnoty \n ");
- getchar();
- return -1;
- }
- cvNamedWindow("video",CV_WINDOW_AUTOSIZE);
- cvNamedWindow("thresh",CV_WINDOW_AUTOSIZE);
- IplImage* recScribble = NULL;
- IplImage* snimok = cvQueryFrame(zaznam);
- cvSetMouseCallback("video", mousehandler );
- while (true)
- {
- snimok = cvQueryFrame(zaznam);
- if (!snimok)
- {
- fprintf( stderr, "Chyba! snimok je bez hodnoty");
- getchar();
- break;
- }
- cvFlip(snimok,snimok,1);
- CvSize cs = cvGetSize(snimok);
- int cam_w = cs.width;
- int cam_h = cs.height;
- IplImage* recYellowThreshold = use_hsv ? GetThresholdedImage(snimok) : GetThresholdedImageRGB(snimok);
- if(do_track)
- {
- cvFindContours(snimok,storage,&cont,sizeof(CvContour), CV_RETR_EXTERNAL, 2, cvPoint(0,0));
- CvRect cvBoundingRect( CvArr* contour, int update );
- CvRect r = cvBoundingRect(cont, 0 );
- CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
- cvMoments(recYellowThreshold, moments, 1);
- double moment10 = cvGetSpatialMoment(moments, 1, 0);
- double moment01 = cvGetSpatialMoment(moments, 0, 1);
- double area = cvGetCentralMoment(moments, 0, 0);
- posX = moment10/area;
- posY = moment01/area;
- printf("position (%d,%d)\n", posX, posY);
- long screen_w = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
- long screen_h = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
- int x = posX * screen_w/cam_w;
- int y = posY * screen_h/cam_h;
- SetCursorPos(x,y);
- }
- else
- {
- if ( clicked_x != -1 )
- {
- snimok->imageData[(clicked_x+clicked_y*cam_w)*3];
- snimok->imageData[(clicked_x+clicked_y*cam_w)*3 +1];
- char * pixel = (snimok->imageData +(clicked_x+clicked_y*cam_w)*3);
- }
- }
- cvShowImage("thresh",recYellowThreshold);
- cvShowImage("video",snimok);
- int k = cvWaitKey(10);
- if ( k == 32) break;
- if ( k == '1' ) { use_hsv = true; printf("hsv\n"); }
- if ( k == '2' ) { use_hsv = false; printf("bgr\n"); }
- if ( k == '3' ) { do_track = true;printf("track on\n"); }
- if ( k == '4' ) { do_track = false; printf("track off\n"); }
- }
- cvReleaseCapture( &zaznam );
- cvDestroyWindow ("Moje Okno");
- return 0;
- }
Add Comment
Please, Sign In to add comment