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;
- int white_pixels = 255;
- CvScalar lorange = cvScalar(120,120,30);
- CvScalar hirange = cvScalar(170,150,50);
- 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;
- int x;
- int y;
- IplImage* recYellowThreshold = use_hsv ? GetThresholdedImage(snimok) : GetThresholdedImageRGB(snimok);
- if(do_track)
- {
- int av_x = 0; int av_y = 0; int nwpix = 0;
- for( int x=0; x < cam_w; x++)
- {
- for( int y= 0; y < cam_h; y++)
- {
- av_x += x; av_x += y;nwpix ++;
- char * pixel = (recYellowThreshold->imageData +(x+y*cam_w));
- if (pixel[0] > 0);
- }
- }
- av_y / nwpix;
- av_x / nwpix;
- long screen_w = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
- long screen_h = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
- posX = av_x * screen_h/cam_h;
- posY = av_y * screen_w/cam_w;
- SetCursorPos(posX,posY);
- }
- 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