float* hough_circles(void* img, int dp, int min_d, int p1, int p2, int min_r, int max_r){ IplImage* image = (IplImage*)img; CvMemStorage* storage = cvCreateMemStorage(0); cvClearMemStorage(storage); CvSeq* circles = cvHoughCircles(image, storage, CV_HOUGH_GRADIENT, dp, min_d, p1, p2, min_r, max_r); if(circles->total == 0) return NULL; float* coords = malloc((1 + 3 * circles->total) * sizeof(float)); coords[0] = (float)circles->total; int i,k; for(i=0, k=1; i<circles->total; i++, k+=3){ float* p = (float*)cvGetSeqElem(circles, i); coords[k] = p[0]; coords[k+1] = p[1]; coords[k+2] = p[2]; } cvReleaseMemStorage(&storage); return coords; }
void EyeTracker::doHoughTransform() { if(!firstDetect) { CvMemStorage* storage = cvCreateMemStorage(0); while(!firstDetect) { float* p; CvSeq* circles; circles = cvHoughCircles(grayEyeImagePts, storage, CV_HOUGH_GRADIENT, 3, grayEyeImagePts->width, 50, 100, 30, 60); int i; static int count = 0; for(i = 0; i < circles->total; i++) { ++count; p = (float*)cvGetSeqElem(circles, i); prev_center.x = (int)p[0]; prev_center.y = (int)p[1]; //cout << "merkez bulundu hough " << count << endl; } firstDetect = true; } cvReleaseMemStorage(&storage); } }
IplImage * BouyObject::FindCircles(const IplImage * imgIn) const { IplImage * imgOut = cvCloneImage(imgIn); CvSize imageSize = cvSize(imgIn->width & -2, imgIn->height & -2 ); IplImage* imgSmallCopy = cvCreateImage( cvSize(imageSize.width/2, imageSize.height/2), IPL_DEPTH_8U, 1 ); cvPyrDown( imgOut, imgSmallCopy); cvPyrUp( imgSmallCopy, imgOut); cvSmooth(imgOut, imgOut, CV_GAUSSIAN, 5); cvReleaseImage(&imgSmallCopy); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* results = cvHoughCircles( imgOut, storage, CV_HOUGH_GRADIENT, 2, imgOut->width/10, 100, 100); cvZero(imgOut); for( int i = 0; i < results->total; i++ ) { float* p = (float*) cvGetSeqElem( results, i ); CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) ); cvCircle( imgOut, pt, cvRound( p[2] ), CV_RGB(0xff,0xff,0xff),CV_FILLED); } cvReleaseMemStorage(&storage); return imgOut; }
int main(int argc, char** argv) { IplImage* image = cvLoadImage( "airplane.jpg", CV_LOAD_IMAGE_GRAYSCALE ); CvMemStorage* storage = cvCreateMemStorage(0); cvSmooth(image, image, CV_GAUSSIAN, 5, 5 ); CvSeq* results = cvHoughCircles( image, storage, CV_HOUGH_GRADIENT, 2, image->width/10 ); for( int i = 0; i < results->total; i++ ) { float* p = (float*) cvGetSeqElem( results, i ); CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) ); cvCircle( image, pt, cvRound( p[2] ), CV_RGB(0xff,0xff,0xff) ); } cvNamedWindow( "cvHoughCircles", 1 ); cvShowImage( "cvHoughCircles", image); cvWaitKey(0); }
void Process::findHoughCircles() { cvClearMemStorage(houghCirclesStorage); cvCvtColor(image, grayImage, CV_RGB2GRAY); cvSmooth(grayImage, grayImage, CV_GAUSSIAN, 5, 5 ); CvSeq* seq = cvHoughCircles( grayImage, houghCirclesStorage, CV_HOUGH_GRADIENT, houghCirclesParam.inverseRatio, houghCirclesParam.minDistance, houghCirclesParam.param1, houghCirclesParam.param2, houghCirclesParam.minRadius, houghCirclesParam.maxRadius ); areas.clear(); for (int i=0; i<seq->total; i++) { float* p = (float*) cvGetSeqElem( seq, i ); Area area; area.pt[0] = p[0]; area.pt[1] = p[1]; area.width = p[2]; area.height = p[2]; areas.push_back(area); } }
/** * Accurately applies circle detection and filtering in order to detect the position * of the dot within an image then, it draws a red rectangle around it in the * original image. * @param img: pointer to the input image * @return PointImage: pointer to a newly created data structure holding the image, * center position of the dot within the image as well as it's radius. */ PointImage* getPoint(IplImage* img) { IplImage* clone = cvCreateImage(cvSize(img->width, img->height), img->depth, 1); cvCvtColor(img, clone, CV_RGB2GRAY); cvThreshold(clone, clone, 127, 255, CV_THRESH_BINARY_INV); cvSmooth(clone, clone, CV_GAUSSIAN, 31, 31); cvMorphologyEx(clone, clone, NULL, NULL, CV_MOP_CLOSE, 6); CvMemStorage* storage = cvCreateMemStorage(); CvSeq* circles = cvHoughCircles(clone, storage, CV_HOUGH_GRADIENT, ((double) (clone->width * clone->height * 5)) / ((double) 964 * 726), 50, 200, 100, 0, 35); float* p; PointImage* out; for (int i = circles->total; i > 0; i--) { p = (float*) cvGetSeqElem(circles, i); if (*I(cvRound(p[0]), cvRound(p[1]), clone) > 185) if (abs(cvRound(p[0]) - (clone->width / 2) * (clone->width / clone->height)) <= 100) { cvRectangle(img, cvPoint(cvRound(p[0] - p[2]), cvRound(p[1] - p[2])), cvPoint(cvRound(p[0] + p[2]), cvRound(p[1] + p[2])), CV_RGB(255,0,0), 3); out = new PointImage(clone, cvPoint(cvRound(p[0]), cvRound(p[1])), cvRound(p[2])); break; } } cvNamedWindow("Circle Detection", CV_WINDOW_AUTOSIZE); cvShowImage("Circle Detection", img); cvReleaseMemStorage(&storage); return out; }
IplImage* BouyObject::ShapeMask(const IplImage * imgIn) const { IplImage * hsv = cvCloneImage(imgIn); IplImage * imgOut = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); //cvEqualizeHist( imgIn, hsv); IplImage * chan0 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan1 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan2 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); IplImage * chan3 = cvCreateImage(cvGetSize(imgIn),IPL_DEPTH_8U, 1); cvCvtColor(imgIn, hsv, CV_BGR2YCrCb); cvSplit(hsv,chan0,chan1,chan2, NULL); CvScalar white = cvRealScalar(255); //imgOut = SegmentationMask(imgIn); imgOut = cvCloneImage(chan2); //invert black and white cvAbsDiffS(imgOut, imgOut, white); // cvShowImage("hue",chan0); // cvShowImage("sat",chan1); // cvShowImage("val",chan2); // cvShowImage("inv",imgOut); //cvWaitKey(0); cvReleaseImage(&hsv); cvReleaseImage(&chan0); cvReleaseImage(&chan1); cvReleaseImage(&chan2); cvReleaseImage(&chan3); CvSize imageSize = cvSize(imgIn->width & -2, imgIn->height & -2 ); IplImage* imgSmallCopy = cvCreateImage( cvSize(imageSize.width/2, imageSize.height/2), IPL_DEPTH_8U, 1 ); cvPyrDown( imgOut, imgSmallCopy); cvPyrUp( imgSmallCopy, imgOut); cvSmooth(imgOut, imgOut, CV_GAUSSIAN, 5); cvReleaseImage(&imgSmallCopy); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* results = cvHoughCircles( imgOut, storage, CV_HOUGH_GRADIENT, 2, imgOut->width/10, 100, 100); cvZero(imgOut); for( int i = 0; i < results->total; i++ ) { float* p = (float*) cvGetSeqElem( results, i ); CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) ); cvCircle( imgOut, pt, cvRound( p[2] ), CV_RGB(0xff,0xff,0xff),CV_FILLED); } cvReleaseMemStorage(&storage); return imgOut; }
void TamatarVision::update() { vidGrabber.grabFrame(); if (vidGrabber.isFrameNew()) { // load image from videograbber colorImg.setFromPixels(vidGrabber.getPixels(), camWidth, camHeight); // convert to grayscale cvCvtColor( colorImg.getCvImage(), grayImg.getCvImage(), CV_RGB2GRAY ); grayImg.flagImageChanged(); // equalize histogram if (doHistEqualize) { cvEqualizeHist(grayImg.getCvImage(), grayImg.getCvImage() ); } // `morphological opening` if (doMorphEx) { int anchor = morphExRadius / 2; structure = cvCreateStructuringElementEx(morphExRadius, morphExRadius, anchor, anchor, CV_SHAPE_ELLIPSE); cvCopy(grayImg.getCvImage(), grayImg2.getCvImage()); cvMorphologyEx(grayImg2.getCvImage(), grayImg.getCvImage(), NULL, structure, CV_MOP_OPEN); } if (doSmoothing) { //grayImg2 = grayImg; //smoothSigmaColor=20; //smoothSigmaSpatial=20; //cvSmooth(grayImg2.getCvImage(), grayImg.getCvImage(), CV_BILATERAL, 9, 9, smoothSigmaColor, smoothSigmaSpatial); cvSmooth(grayImg.getCvImage(), grayImg.getCvImage(), CV_GAUSSIAN, 3, 3, 2, 2); } //grayImg.threshold(120); // threshold if (doThreshold) { // grayImg.threshold(threshold); grayImg2 = grayImg; cvThreshold(grayImg2.getCvImage(), grayImg.getCvImage(), threshold, thresholdMax, CV_THRESH_TOZERO); // cvAdaptiveThreshold(grayImg2.getCvImage(), grayImg.getCvImage(), threshold, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_, 3, 5); } if (doCanny) { cvCanny(grayImg.getCvImage(), grayImg.getCvImage(), cannyThres1, cannyThres2, 3); } //cvCanny5grayImg.getCvImage(),grayImg.getCvImage(), 120, 180, 3); //cvSobel(grayImg.getCvImage(), grayImg.getCvImage(), 1, 1, 3); if (doCircles) { CvMemStorage* storage = cvCreateMemStorage(0); circles = cvHoughCircles(grayImg.getCvImage(), storage, CV_HOUGH_GRADIENT, 2, grayImg.getHeight()/4, circleEdgeThres, circleAccThres, circleMinRadius, circleMaxRadius); } if (doContours) { contourFinder.findContours(grayImg, 10, (camWidth*camHeight)/2, 20, false, true); } } }
void detectPupils(IplImage *img) { //cvShowImage("Eyes before", img); //setting up the grayscale image IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 ); IplImage* copy = cvCreateImage( cvGetSize(img), 8, 3 ); //IplImage* src = cvCreateImage(cvGetSize(img), 8, 1); //set up circle storage CvMemStorage* storage = cvCreateMemStorage(0); //prepair the grayscale image cvCvtColor( img, gray, CV_RGB2GRAY ); //cvCopy(img, copy); /*try close elipse start*/ ///cvThreshold(gray, gray, 30, 255, CV_THRESH_BINARY_INV); cvShowImage("Eyes before", gray); //IplConvKernel elipse = *cvCreateStructuringElementEx(3, 3, 0, 0, CV_SHAPE_ELLIPSE, 0); //cvMorphologyEx(gray, gray, copy, &elipse, CV_MOP_CLOSE, 2); /*try close elipse end */ //cvThreshold(gray, gray, 42, 255, CV_THRESH_BINARY_INV); //cvAdaptiveThreshold(gray, gray, 255); cvConvertScaleAbs(gray, gray, 4, -3); cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9); // smooth it, otherwise a lot of false circles may be detected //cvCanny(gray, gray, 0, canny2, canny3); //cvThreshold(gray, gray, 200, 255, CV_THRESH_BINARY); //apply Hough CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, dp, 10, param1, param2, 0, gray->height/3); //loop through circles for(int i = 0; i < circles->total; i++) { float* circle = (float*)cvGetSeqElem( circles, i ); cvCircle( img, cvPoint(cvRound(circle[0]),cvRound(circle[1])), cvRound(circle[2]), CV_RGB(0,255,0), 1, 8, 0 ); } cvShowImage("Eyes", gray); //std::cerr << "Circles: " << circles->total << std::endl; }
void houghCallBack(int pos){ if ( pos ){ IplImage *hsv = convertRGB2HSV(src); if ( hsv ){ IplImage *thresholded = cvCreateImage(cvGetSize(src),src->depth,1); CvScalar hsv_min = cvScalar(hue_min_value, sat_min_value, val_min_value); CvScalar hsv_max = cvScalar(hue_max_value, sat_max_value, val_max_value); cvInRangeS(hsv,hsv_min,hsv_max,thresholded); //cvSaveImage("thresholded.jpg",thresholded); CvMemStorage *storage = cvCreateMemStorage(0); CvSeq* circles = cvHoughCircles(thresholded,storage,CV_HOUGH_GRADIENT,2,hough_minDist_value,hough_param1_value,hough_param2_value,hough_minRadius_value,hough_maxRadius_value); printf("found %d circles\n",circles->total); int i; for ( i=0; i<circles->total; i++ ){ float *p = (float*)cvGetSeqElem(circles,i); printf("\tcircle%d x:%f y:%f r:%f\n",i,p[0],p[1],p[2]); cvCircle(hsv,cvPoint(cvRound(p[0]),cvRound(p[1])),cvRound(p[2]),CV_RGB(255,0,0),3,8,0); } cvNamedWindow("Hough"); cvShowImage("Hough",hsv); cvNamedWindow("thresholdedByHSV"); cvShowImage("thresholdedByHSV",thresholded); //cvSaveImage("result.jpg",src_backup); printf("enter some key to kill Hough result...\n"); cvWaitKey(); cvDestroyWindow("thresholdedByHSV"); cvDestroyWindow("Hough"); cvReleaseImage(&hsv); cvReleaseImage(&thresholded); } } }
int main(int argc, char** argv) { IplImage* image = cvLoadImage( "4.jpg" ); //IplImage* src = cvLoadImage("index.jpg"); IplImage* croped=cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 1 ); IplImage* src = cvCreateImage( cvGetSize(image), IPL_DEPTH_8U, 1 ); IplImage* canny=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1); IplImage* rgbcanny=cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,3); CvMemStorage* storage = cvCreateMemStorage(0); cvCvtColor( image, src, CV_BGR2GRAY ); cvSmooth(src, src, CV_GAUSSIAN, 5, 5 ); //cvCanny(src,canny,50,200,3); CvSeq* results = cvHoughCircles( src, storage, CV_HOUGH_GRADIENT, 2, src->height/4, 200, 100 //src->height/20, //src->height/2 ); cvCvtColor(canny,rgbcanny,CV_GRAY2BGR); printf("%d",results->total); for( int i = 0; i < results->total; i++ ) { float* p = (float*) cvGetSeqElem( results, i ); CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) ); croped=crop(image,pt,cvRound(p[2])); cvCircle( image, pt, cvRound( p[2] ), CV_RGB(255,0,0) ); } cvNamedWindow( "HoughCircles", 1 ); cvShowImage( "HoughCircles", image); if(croped){ cvNamedWindow("crop",1); cvShowImage("crop",croped);} cvWaitKey(0); }
int main(int argc, char** argv) { cvNamedWindow("image"); IplImage * src = cvLoadImage("../cvtest/7newsample.jpg", 0); IplImage * temp = cvCreateImage(cvGetSize(src), 8,1); IplImage * img=cvCreateImage(cvGetSize(src), 8, 1); cvCopyImage(src,temp); cvCopyImage(src, img); cvSmooth(img,img); IplConvKernel *element = 0; //定义形态学结构指针 element = cvCreateStructuringElementEx(3,3, 1, 1, CV_SHAPE_ELLIPSE, 0);//3,5,7 cvErode( src, src, element); //形态梯度 cvMorphologyEx( src, img, img, element,//NULL, //default 3*3 CV_MOP_GRADIENT, 1); cvShowImage("image", img); cvWaitKey(0); IplImage* image=img; //= cvLoadImage( "../cvtest/7newsample.jpg", CV_LOAD_IMAGE_GRAYSCALE ); IplImage* src2 =img;//= cvLoadImage( "../cvtest/7newsample.jpg"); //Changed for prettier show in color CvMemStorage* storage = cvCreateMemStorage(0); cvSmooth(image, image, CV_GAUSSIAN, 5, 5 ); CvSeq* results = cvHoughCircles( image, storage, CV_HOUGH_GRADIENT, 4, image->width/10 ); for( int i = 0; i < results->total; i++ ) { float* p = (float*) cvGetSeqElem( results, i ); CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) ); cvCircle( src2, pt, cvRound( p[2] ), CV_RGB(0xff,0,0) ); } cvNamedWindow( "cvHoughCircles", 1 ); cvShowImage( "cvHoughCircles", src2); cvWaitKey(0); }
int main(int argc, char** argv) { IplImage* image = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE); IplImage* src = cvLoadImage(argv[1]); //Changed for prettier show in color CvMemStorage* storage = cvCreateMemStorage(0); cvSmooth(image, image, CV_GAUSSIAN, 5, 5); CvSeq* results = cvHoughCircles(image, storage, CV_HOUGH_GRADIENT, 4, image->width / 10); for (int i = 0; i < results->total; i++) { float* p = (float*) cvGetSeqElem(results, i); CvPoint pt = cvPoint(cvRound(p[0]), cvRound(p[1])); cvCircle(src, pt, cvRound(p[2]), CV_RGB(0xff,0,0)); } cvNamedWindow("cvHoughCircles", 1); cvShowImage("cvHoughCircles", src); cvWaitKey(0); }
vector <Ball> getBalls(Mat img_in){ vector<Ball> balls; IplImage* img_in1 = new IplImage(img_in); CvMemStorage* storage = cvCreateMemStorage(0); // hough detector works better with some smoothing of the image cvSmooth( img_in1,img_in1, CV_GAUSSIAN, 9, 9 ); // Element to define dilation shape (basically a matrix) IplConvKernel* element = cvCreateStructuringElementEx(9,9, 4, 4, CV_SHAPE_ELLIPSE); // Suurenda veits palli (valgeid alasid) cvDilate(img_in1, img_in1 ,element , 4); CvSeq* circles = cvHoughCircles(img_in1, storage, CV_HOUGH_GRADIENT, 2, img_in1->height/4, 100, 50, 10, 400); for (int i = 0; i < circles-> total; i++) { float* p = (float*)cvGetSeqElem( circles, i ); printf("Ball! x=%f y=%f r=%f\n\r" ,p[0],p[1],p[2] ); Ball current_ball = Ball(p[0],p[1],p[2]); cout << " X, Y radiaanides " << current_ball.getX_degR() <<" "<< current_ball.getY_degR() << endl; balls.push_back(current_ball); circle( img_in, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(255,255,0), -1, 8, 0 ); circle( img_in, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } sort(balls.begin(),balls.end()); return balls; }
CvSeq* HoughCircle::locateHoughCircles(IplImage* img,double min_distance, double upperThreshold, double accumulatorThreshold, double min_radius, double max_radius ){ //IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); //storage = cvCreateMemStorage(0); IplImage* gray=cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); CvMemStorage* storage = cvCreateMemStorage(0); cvCvtColor( img, gray, CV_BGR2GRAY ); // cvShowImage("Gray",gray); // cvEqualizeHist(gray,gray); // cvShowImage("Hist Eq Gray",gray); CvSeq* circle = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, min_distance, upperThreshold, accumulatorThreshold, min_radius, max_radius); cvReleaseImage(&gray); cvReleaseMemStorage(&storage); return circle; }
void drawCircleAndCrop(char *pFileName, IplImage* img) { IplImage* cpy = cvCloneImage( img ); IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 ); cvCvtColor( img, gray, CV_BGR2GRAY ); cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 200); int i; for( i = 0; i < circles->total; i++ ) { float* p = (float*)cvGetSeqElem( circles, i ); cvCircle( gray, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( gray, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } // show the resultant image cvShowImage( wndname, gray ); cvReleaseImage(&cpy); cvReleaseImage(&gray); }
int mapFunction::circleDetect(Mat image1){ IplImage* img = new IplImage(image1); IplImage* gray = cvCreateImage( cvGetSize(img), img->depth, 1); CvMemStorage* storage = cvCreateMemStorage(0); cvCvtColor( img, gray, CV_RGB2GRAY ); //cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 ); CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100); /*for (int i = 0; i < circles->total; i++) { float* p = (float*)cvGetSeqElem( circles, i ); cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } cvNamedWindow( "circles", 1 ); cvShowImage( "circles", img ); waitKey(0);*/ if(circles->total==0) return 0; else return 1; }
int main(int argc, char* argv[]) { // Default capture size - 640x480 CvSize size = cvSize(640,480); // Open capture device. 0 is /dev/video0, 1 is /dev/video1, etc. CvCapture* capture = cvCaptureFromCAM( 0 ); if( !capture ) { fprintf( stderr, "ERROR: capture is NULL \n" ); getchar(); return -1; } // Create a window in which the captured images will be presented cvNamedWindow( "Camera", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "EdgeDetection", CV_WINDOW_AUTOSIZE ); // Detect a red ball int hl = 60, hu = 75, sl = 255, su = 256, vl = 170, vu = 256; IplImage * hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); IplImage* thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1); while( 1 ) { // Get one frame IplImage* frame = cvQueryFrame( capture ); if( !frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } CvScalar hsv_min = cvScalar(hl, sl, vl, 0); CvScalar hsv_max = cvScalar(hu, su, vu, 0); // Covert color space to HSV as it is much easier to filter colors in the HSV color-space. cvCvtColor(frame, hsv_frame, CV_RGB2HSV); // Filter out colors which are out of range. cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded); // Memory for hough circles CvMemStorage* storage = cvCreateMemStorage(0); // hough detector works better with some smoothing of the image cvDilate(thresholded, thresholded, NULL, 3); cvSmooth( thresholded, thresholded, CV_GAUSSIAN, 9, 9 ); cvErode(thresholded, thresholded, NULL, 3); CvSeq* circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 1, thresholded->height/4, 400, 10, 20, 400); int maxRadius = 0; int x = 0, y = 0; bool found = false; for (int i = 0; i < circles->total; i++) { float* p = (float*)cvGetSeqElem( circles, i ); if (p[2] > maxRadius){ maxRadius = p[2]; x = p[0]; y = p[1]; found = true; } } if (found){ //printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] ); cvCircle( frame, cvPoint(cvRound(x),cvRound(y)), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( frame, cvPoint(cvRound(x),cvRound(y)), cvRound(maxRadius), CV_RGB(255,0,0), 3, 8, 0 ); } cvShowImage( "Camera", frame ); // Original stream with detected ball overlay cvShowImage( "HSV", hsv_frame); // Original stream in the HSV color space cvShowImage( "After Color Filtering", thresholded ); // The stream after color filtering cvReleaseMemStorage(&storage); // Do not release the frame! //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator int key = cvWaitKey(10); switch(key){ case 'q' : hu += 5; break; case 'Q' : hu -= 5; break; case 'a' : hl -= 5; break; case 'A' : hl += 5; break; case 'w' : su += 5; break; case 'W' : su -= 5; break; case 's' : sl -= 5; break; case 'S' : sl += 5; break; case 'e' : vu += 5; break; case 'E' : vu -= 5; break; case 'd' : vl -= 5; break; case 'D' : vl += 5; break; } if (key != -1){ printf("H: %i, S: %i, V: %i\nH: %i, S: %i, V: %i\n\n", hu, su, vu, hl, sl, vl); } } // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyWindow( "mywindow" ); return 0; }
void process_image(){ /////////////////////////////////////////////////////// //////////////////// PUPIL///////////////////////////// /////////////////////////////////////////////////////// int numBins = 256; float range[] = {0, 255}; float *ranges[] = { range }; CvHistogram *hist = cvCreateHist(1, &numBins, CV_HIST_ARRAY, ranges, 1); cvClearHist(hist); cvCalcHist(&smooth, hist, 0, 0); IplImage* imgHist = DrawHistogram(hist,1,1); cvClearHist(hist); //cvShowImage("hist", imgHist); cvThreshold(smooth,pupil,50,255,CV_THRESH_BINARY); //cvShowImage( "pupi_binary",pupil); cvCanny(pupil,pedge,40,50); //cvShowImage( "pupil_edge",pedge); ////////////////////////////////////////////////////////// //////////////////////IRIS//////////////////////////////// ////////////////////////////////////////////////////////// //cvEqualizeHist(smooth,smooth); //cvShowImage("Equalized",smooth); cvThreshold(smooth,iris,100,255,CV_THRESH_BINARY); //115 //cvShowImage( "iris_binary",iris); //cvSobel(iris,iedge,1,0,3); cvCanny(iris,iedge,1,255); //cvShowImage( "iris_edge",iedge); ///////////////////////////////////////////////////////// ///////////////////////Eyelids/////////////////////////// ///////////////////////////////////////////////////////// cvThreshold(smooth,eyelid_mask,150,255,CV_THRESH_OTSU); cvNot(eyelid_mask,eyelid_mask); //cvShowImage("eyelid",eyelid_mask); //cvAdaptiveThreshold(smooth,contour,255,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY,9,1); //cvThreshold(smooth,contour,130,255,CV_THRESH_BINARY); //cvShowImage( "contour",contour); //CvSeq* firstContour = NULL; //CvMemStorage* cstorage = cvCreateMemStorage(0); //cvFindContours(con, cstorage, &firstContour,sizeof(CvContour), CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE); //cvDrawContours(dst,firstContour,CV_RGB(0,255,0),CV_RGB(0,0,255),10,2,8); CvMemStorage* storage_pupil = cvCreateMemStorage(0); CvSeq* presults = cvHoughCircles(pedge,storage_pupil,CV_HOUGH_GRADIENT,2,src->width,255,1); for( int i = 0; i < presults->total; i++ ) { float* p = (float*) cvGetSeqElem( presults, i ); CvPoint pt = cvPoint( cvRound( p[0] ),cvRound( p[1] ) ); xp=cvRound( p[0] ); yp=cvRound( p[1] ); rp=p[2]; cvCircle(dst,pt,cvRound( p[2] ),CV_RGB(0,255,255),1,400); xroi= xp-shift; yroi= yp-shift; cvRectangle(dst,cvPoint(( p[0] )-shift,p[1]-shift),cvPoint(( p[0] )+shift,p[1]+shift),CV_RGB(255,0,255), 1); CvRect roi= cvRect(xroi ,yroi,shift*2,shift*2); cvSetImageROI( iedge, roi ); //cvShowImage("ROI",iedge); } //////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////// CvMemStorage* storage_iris = cvCreateMemStorage(0); CvSeq* iresults = cvHoughCircles(iedge,storage_iris,CV_HOUGH_GRADIENT,2,src->width,1,50,50); for( int i = 0; i < iresults->total; i++ ) { float* p = (float*) cvGetSeqElem( iresults, i ); CvPoint pt = cvPoint( cvRound( p[0] )+xroi,cvRound( p[1] )+yroi ); cvCircle(dst,pt,cvRound( p[2] ),CV_RGB(255,0,0),1,400); xi=cvRound( p[0] )+xroi; yi=cvRound( p[1] )+yroi; ri=(p[2]); cvCircle(iris_mask,pt,cvRound( p[2] ),CV_RGB(255, 255, 255),-1, 8, 0); //cvShowImage("iris_mask",iris_mask); } /////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////// /////////////////////////////////////////// cvResetImageROI(iedge); cvAnd(dst,dst,res,iris_mask); //cvShowImage("iris_mask",res); cvAnd(res,res, mask, eyelid_mask); //cvShowImage("Mask",mask); //cvLogPolar(mask,finalres,cvPoint2D32f (xp,yp),100, CV_INTER_LINEAR ); //cvShowImage("Final Result",finalres); ///////////////////////////////////////////////////////// ///////////////////////////////////////////////////////// ///////////////////////////////////////////////////////// /* */ }
void testApp::houghCircles( ofxCvGrayscaleImage sourceImg) { IplImage* gray = sourceImg.getCvImage(); cvSmooth( gray, gray, CV_GAUSSIAN, 5, 5 ); // smooth it, otherwise a lot of false circles may be detected CvMemStorage* storage = cvCreateMemStorage(0); circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 2, gray->width/8, 300, 200 ); circCount = circles->total; // find positions of CV circles for (int i = 0; i < circles->total; i++) { float* p = (float*)cvGetSeqElem( circles, i ); ofPoint pos; pos.x = cvPoint( cvRound(p[0]),cvRound(p[1]) ).x; pos.y = cvPoint( cvRound(p[0]),cvRound(p[1]) ).y; pos.x = (int)pos.x; pos.y = (int)pos.y; int radius = cvRound( p[2] ); bool cFound = false; for (int circs = 0; circs < myCircles.size(); circs++) { ofPoint& posCirc = myCircles[circs].pos; float dist = ofDistSquared(pos.x, pos.y, posCirc.x, posCirc.y); //cout << "distance is: " << dist << endl; // check to see if there is a circle near an existing tracked circle if ( dist < 1000 ) { myCircles[circs].lastSeen = ofGetFrameNum(); myCircles[circs].radius = radius; myCircles[circs].pos = pos; myCircles[circs].isAlive += 1; cFound = true; } } if (!cFound) { radius *=1.05; CircleTrack c = CircleTrack( pos ); c.pos = pos; c.radius = radius; c.lastSeen = ofGetFrameNum(); c.isAlive = 0; c.drawMe = false; c.isSetUp = false; c.iD = circID; if (c.radius) { myCircles.push_back(c); circID++; ofPixels pixCopy = pix; pixCopy.crop( pos.x-radius, (pos.y-radius), radius*2 , radius*2 ); tex.loadData( pixCopy ); ofTexture T; T.allocate(radius*2,radius*2, GL_RGB); T.loadData(pixCopy); particle P = particle( pos,circID,radius,T ); punched.push_back( P ); printf("punched %f,%f\n", pos.x,pos.y); } } } cvReleaseMemStorage(&storage); for ( int x = 0; x<myCircles.size(); x++ ) { bool isSetupNow = myCircles[x].isSetUp; if ( !isSetupNow ) { myCircles[x].setup(); } } vector<CircleTrack>::iterator iter = myCircles.begin(); while (iter != myCircles.end()) { double life = iter->lastSeen; int isAlive = iter->isAlive; // kill old particles; if ( (ofGetFrameNum()-life) > 50 ) { ofPoint tracePos = iter->pos; iter = myCircles.erase(iter); cout << "shape deleted at: "<< tracePos.x << ", " << tracePos.y << endl; } else { if (iter->isAlive > 0) // used to be 3? { iter->drawMe = true; } else { iter->drawMe = false; } iter++; } } }
void Run() { int w, h; IplImage *pCapImage; PBYTE pCapBuffer = NULL; // Create camera instance _cam = CLEyeCreateCamera(_cameraGUID, _mode, _resolution, _fps); if(_cam == NULL) return; // Get camera frame dimensions CLEyeCameraGetFrameDimensions(_cam, w, h); // Depending on color mode chosen, create the appropriate OpenCV image if(_mode == CLEYE_COLOR_PROCESSED || _mode == CLEYE_COLOR_RAW) pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 4); else pCapImage = cvCreateImage(cvSize(w, h), IPL_DEPTH_8U, 1); // Set some camera parameters //CLEyeSetCameraParameter(_cam, CLEYE_GAIN, 30); //CLEyeSetCameraParameter(_cam, CLEYE_EXPOSURE, 500); //CLEyeSetCameraParameter(_cam, CLEYE_AUTO_EXPOSURE, false); //CLEyeSetCameraParameter(_cam, CLEYE_AUTO_GAIN, false); //CLEyeSetCameraParameter(_cam, CLEYE_AUTO_WHITEBALANCE, false); //CLEyeSetCameraParameter(_cam, CLEYE_WHITEBALANCE_RED, 100); //CLEyeSetCameraParameter(_cam, CLEYE_WHITEBALANCE_BLUE, 200); //CLEyeSetCameraParameter(_cam, CLEYE_WHITEBALANCE_GREEN, 200); // Start capturing CLEyeCameraStart(_cam); CvMemStorage* storage = cvCreateMemStorage(0); IplImage* hsv_frame = cvCreateImage(cvSize(pCapImage->width, pCapImage->height), IPL_DEPTH_8U, 3); IplImage* thresholded = cvCreateImage(cvSize(pCapImage->width, pCapImage->height), IPL_DEPTH_8U, 1); IplImage* temp = cvCreateImage(cvSize(pCapImage->width >> 1, pCapImage->height >> 1), IPL_DEPTH_8U, 3); // Create a window in which the captured images will be presented cvNamedWindow( "Camera" , CV_WINDOW_AUTOSIZE ); cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "EdgeDetection", CV_WINDOW_AUTOSIZE ); //int hl = 100, hu = 115, sl = 95, su = 135, vl = 115, vu = 200; int hl = 5, hu = 75, sl = 40, su = 245, vl = 105, vu = 175; // image capturing loop while(_running) { // Detect a red ball CvScalar hsv_min = cvScalar(hl, sl, vl, 0); CvScalar hsv_max = cvScalar(hu, su, vu, 0); cvGetImageRawData(pCapImage, &pCapBuffer); CLEyeCameraGetFrame(_cam, pCapBuffer); cvConvertImage(pCapImage, hsv_frame); // Get one frame if( !pCapImage ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } // Covert color space to HSV as it is much easier to filter colors in the HSV color-space. cvCvtColor(pCapImage, hsv_frame, CV_RGB2HSV); // Filter out colors which are out of range. cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded); // Memory for hough circles CvMemStorage* storage = cvCreateMemStorage(0); // hough detector works better with some smoothing of the image cvSmooth( thresholded, thresholded, CV_GAUSSIAN, 9, 9 ); CvSeq* circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 2, thresholded->height/4, 100, 50, 10, 400); for (int i = 0; i < circles->total; i++) { float* p = (float*)cvGetSeqElem( circles, i ); //printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] ); cvCircle( pCapImage, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( pCapImage, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } cvShowImage( "Camera", pCapImage ); // Original stream with detected ball overlay cvShowImage( "HSV", hsv_frame); // Original stream in the HSV color space cvShowImage( "EdgeDetection", thresholded ); // The stream after color filtering cvReleaseMemStorage(&storage); // Do not release the frame! //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator int key = cvWaitKey(10); switch(key){ case 'q' : hu += 5; break; case 'Q' : hu -= 5; break; case 'a' : hl -= 5; break; case 'A' : hl += 5; break; case 'w' : su += 5; break; case 'W' : su -= 5; break; case 's' : sl -= 5; break; case 'S' : sl += 5; break; case 'e' : vu += 5; break; case 'E' : vu -= 5; break; case 'd' : vl -= 5; break; case 'D' : vl += 5; break; } if (key != -1){ printf("H: %i, S: %i, V: %i\nH: %i, S: %i, V: %i\n\n", hu, su, vu, hl, sl, vl); } } cvReleaseImage(&temp); cvReleaseImage(&pCapImage); // Stop camera capture CLEyeCameraStop(_cam); // Destroy camera object CLEyeDestroyCamera(_cam); // Destroy the allocated OpenCV image cvReleaseImage(&pCapImage); _cam = NULL; }
int main(int argc, char* argv[]) { int i; CvSize size = cvSize(640,480); CvCapture* capture = cvCaptureFromCAM(0); if(!capture) { fprintf(stderr, "Error in opening the Camera.\n"); exit(1); } cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640); cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480); cvNamedWindow("Camera", CV_WINDOW_AUTOSIZE); cvNamedWindow("HSV", CV_WINDOW_AUTOSIZE); cvNamedWindow("Thresholded", CV_WINDOW_AUTOSIZE); IplImage* frame = cvQueryFrame(capture); if(!frame) exit(1); IplImage* hsv = cvCreateImage(size, IPL_DEPTH_8U, 3); IplImage* thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1); CvScalar hsv_min = cvScalar(170, 120, 100, 0); CvScalar hsv_max = cvScalar(358, 256, 256, 0); int xpre = 0, ypre = 0; while(1) { frame = cvQueryFrame(capture); if(!frame) { fprintf(stderr, "Failed to capture an Image.\n"); break; } cvCvtColor(frame, hsv, CV_BGR2HSV); cvInRangeS(hsv, hsv_min, hsv_max, thresholded); cvSmooth(thresholded, thresholded, CV_GAUSSIAN, 9,9,0,0); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 2,thresholded->height/4, 100, 50, 40, 400); /*The entire code lies in this line, This function detects the actual circles in the image. The parameters are as follows: thresholded - input image storage - memory storage that stores location of circles CV_HOUGH_GRADIENT - method used to detect circles(only one present) 2(dp) - Resolution for the detection, more the better (thresholded->height)/4(minDist) - Minimum Distance between the circles 100(param1) - For the Canny Edge Detection 50(param2) - For the Canny Edge Detection 10(minRadius) - Minimum radius of Circle 400(maxRadius) - Maximum radius of Circle Link Followed for above information - http://www.emgu.com/wiki/files/1.3.0.0/html/0ac8f298-48bc-3eee-88b7-d2deed2a285d.htm */ for(i = 0;i<circles->total;i++) { float* p = (float*)cvGetSeqElem( circles, i ); printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] ); if(xpre == 0 && ypre == 0) { xpre = p[0]; ypre = p[1]; cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } else { if((abs(p[0] - xpre) < 100) && (abs(p[1] - ypre) < 100)) { cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } else; } xpre = p[0]; ypre = p[1]; } /*cvGetSeqElem(circles,i) - This gets each sequence element form the memory storage and puts in the pointer p. Now p has three variables (p[0],p[1]) - (x,y) coordinates of circle p[2] - radius of the circle Link Followed - http://opencv.willowgarage.com/documentation/dynamic_structures.html */ cvShowImage("Camera", frame); cvShowImage("HSV", hsv); cvShowImage("Thresholded", thresholded); char c=cvWaitKey(33); if(c == 27) break; } cvDestroyAllWindows(); cvReleaseCapture(&capture); cvReleaseImage(&frame); cvReleaseImage(&hsv); }
int run(const char *serverAddress, const int serverPort, char headless) { int i, sockfd, show = ~0; int frames = 0; int returnValue = EXIT_SUCCESS; CvCapture *capture; CvMemStorage *storage; IplImage *grabbedImage; IplImage *imgThreshold; CvSeq *seq; CvFont font; SendQueue *queue; char strbuf[255]; struct timeval oldTime, time, diff; float lastKnownFPS = 0; sockfd = initNetwork(serverAddress, serverPort); if (sockfd == -1) { fprintf(stderr, "ERROR: initNetwork returned -1\n"); return EXIT_FAILURE; } queue = initSendQueue(); capture = cvCaptureFromCAM(CV_CAP_ANY); if (capture == NULL) { fprintf( stderr, "ERROR: capture is NULL \n" ); getchar(); return EXIT_FAILURE; } // Create a window in which the captured images will be presented cvNamedWindow("mywindow", CV_WINDOW_AUTOSIZE); storage = cvCreateMemStorage(0); // void cvInitFont(font, font_face, hscale, vscale, shear=0, thickness=1, line_type=8 ) cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1, 1, 0, 1, 8); gettimeofday(&oldTime, NULL); // Show the image captured from the camera in the window and repeat while (1) { cvClearMemStorage(storage); grabbedImage = cvQueryFrame(capture); if (grabbedImage == NULL) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); returnValue = EXIT_FAILURE; break; } //Create detection image imgThreshold = cvCreateImage(cvGetSize(grabbedImage), 8, 1); cvInRangeS(grabbedImage, min, max, imgThreshold); //Flip images to act as a mirror. //TODO remove when camera faces screen if (show) { cvFlip(grabbedImage, grabbedImage, 1); cvFlip(imgThreshold, imgThreshold, 1); } //Find all dots in the image. This is where any calibration of dot detection is done, if needed, though it //should be fine as it is right now. /* * image, circleStorage, method, double dp, double minDist, double param1, double param2, int minRadius, int maxRadius */ seq = cvHoughCircles(imgThreshold, storage, CV_HOUGH_GRADIENT, 2, 20, 20, 2, 0, 10); for (i = 0; i < seq->total; i++){ // Get point float *p = (float*)cvGetSeqElem(seq, i); //Draw current circle to the original image if (show) paintCircle(p, grabbedImage); //Buffer current circle to be sent to the server addPointToSendQueue(p, queue); } //Print some statistics to the image if (show) { snprintf(strbuf, sizeof(strbuf), "Dots: %i", seq->total); cvPutText(grabbedImage, strbuf, cvPoint(10, 20), &font, cvScalar(WHITE)); snprintf(strbuf, sizeof(strbuf), "FPS: %.1f", lastKnownFPS); cvPutText(grabbedImage, strbuf, cvPoint(10, 200), &font, cvScalar(WHITE)); } //Show images //TODO Comment these out will probably improve performance quite a bit if (show) { cvShowImage("mywindow", imgThreshold); cvShowImage("mywindow", grabbedImage); } gettimeofday(&time, NULL); timeval_subtract(&diff, &time, &oldTime); // printf("Frames = %i\n", diff.tv_sec); if (diff.tv_sec >= 2) { lastKnownFPS = (float)frames / diff.tv_sec; oldTime = time; frames = 0; } //Add one to the frame rate counter frames++; //Send to dots detected this frame to the server sendQueue(sockfd, queue); clearSendQueue(queue); // //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator i = (cvWaitKey(10) & 0xff); if (i == 'v') show = ~show; if (i == 27) break; } // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyWindow( "mywindow" ); destroySendQueue(queue); close(sockfd); return returnValue; }
// -------------------------------------------------------------------------- // main(Number of arguments, Argument values) // Description : This is the entry point of the program. // Return value : SUCCESS:0 ERROR:-1 // -------------------------------------------------------------------------- int main(int argc, char **argv) { // AR.Drone class ARDrone ardrone; // Initialize if (!ardrone.open()) { printf("Failed to initialize.\n"); return -1; } // Get a image IplImage* image = ardrone.getImage(); // Images IplImage *gray = cvCreateImage(cvGetSize(image), image->depth, 1); IplImage *smooth = cvCreateImage(cvGetSize(image), image->depth, 1); IplImage *canny = cvCreateImage(cvGetSize(image), image->depth, 1); // Canny thresholds int th1 = 50, th2 = 100; cvNamedWindow("canny"); cvCreateTrackbar("th1", "canny", &th1, 255); cvCreateTrackbar("th2", "canny", &th2, 255); // Main loop while (1) { // Key input int key = cvWaitKey(1); if (key == 0x1b) break; // Update if (!ardrone.update()) break; // Get an image image = ardrone.getImage(); // Convert to gray scale cvCvtColor(image, gray, CV_BGR2GRAY); // De-noising cvSmooth(gray, smooth, CV_GAUSSIAN, 23, 23); // Detect edges cvCanny(smooth, canny, th1, th2, 3); // Detect circles CvMemStorage *storage = cvCreateMemStorage(0); CvSeq *circles = cvHoughCircles(smooth, storage, CV_HOUGH_GRADIENT, 1.0, 10.0, MAX(th1,th2), 20); // Draw circles for (int i = 0; i < circles->total; i++) { float *p = (float*) cvGetSeqElem(circles, i); cvCircle(image, cvPoint(cvRound(p[0]), cvRound(p[1])), cvRound(p[2]), CV_RGB(0,255,0), 3, 8, 0); } // Release memory cvReleaseMemStorage(&storage); // Change camera static int mode = 0; if (key == 'c') ardrone.setCamera(++mode%4); // Display the image cvShowImage("camera", image); cvShowImage("canny", canny); } // Release memories cvReleaseImage(&gray); cvReleaseImage(&smooth); cvReleaseImage(&canny); // See you ardrone.close(); return 0; }
int main(int argc, char* argv[]) { // Default capture size - 640x480<br /> CvSize size = cvSize(640,480); // Open capture device. 0 is /dev/video0, 1 is /dev/video1, etc. CvCapture* capture = cvCaptureFromCAM( 0 ); if( !capture ) { fprintf( stderr, "ERROR: capture is NULL \n" ); getchar(); return -1; } // Create a window in which the captured images will be presented cvNamedWindow( "Camera", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "EdgeDetection", CV_WINDOW_AUTOSIZE ); // Detect a red ball CvScalar hsv_min = cvScalar(150, 84, 130, 0); CvScalar hsv_max = cvScalar(358, 256, 255, 0); IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); IplImage* thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1); while( 1 ) { // Get one frame IplImage* frame = cvQueryFrame( capture ); if( !frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } // Covert color space to HSV as it is much easier to filter colors in the HSV color-space. cvCvtColor(frame, hsv_frame, CV_BGR2HSV); // Filter out colors which are out of range. cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded); // Memory for hough circles CvMemStorage* storage = cvCreateMemStorage(0); // hough detector works better with some smoothing of the image cvSmooth( thresholded, thresholded, CV_GAUSSIAN, 9, 9 ); CvSeq* circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 2, thresholded->height/4, 100, 50, 10, 400); for (int i = 0; i < circles->total; i++) { float* p = (float*)cvGetSeqElem( circles, i ); printf("Ball! x=%f y=%f r=%f\n\r", p[0],p[1],p[2] ); cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), 3, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 ); } cvShowImage( "Camera", frame ); // Original stream with detected ball overlay cvShowImage( "HSV", hsv_frame); // Original stream in the HSV color space cvShowImage( "After Color Filtering", thresholded ); // The stream after color filtering</p> cvReleaseMemStorage(&storage); // Do not release the frame! //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator if( (cvWaitKey(10) & 255) == 27 ) break; } // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyWindow( "mywindow" ); return 0; }
// initialize the main function int main(int argc, char *argv[]) { if (argc < 2) { printf("Usage: %s <img.jpg>\n", argv[0]); return 1; } IplImage* picture = cvLoadImage(argv[1]); IplImage* greyImg = cvCreateImage(cvGetSize(picture), IPL_DEPTH_8U, 1); IplImage* cannyImg = cvCreateImage(cvGetSize(picture), IPL_DEPTH_8U, 1); IplImage* drawnImg = cvCreateImage(cvGetSize(picture), IPL_DEPTH_8U, 3); IplImage* contrastImg = cvCreateImage(cvGetSize(picture), IPL_DEPTH_8U, 1); cvNamedWindow("Image", CV_WINDOW_AUTOSIZE); cvNamedWindow("Canny", CV_WINDOW_AUTOSIZE); cvNamedWindow("Threshold", CV_WINDOW_NORMAL); cvCvtColor(picture, greyImg, CV_BGR2GRAY); cvEqualizeHist(greyImg, greyImg); CvMemStorage* storage = cvCreateMemStorage(0); while (1) { // Create trackbars cvCopy(picture, drawnImg); // picture to be displayed cvCreateTrackbar( "min_dist", "Image", &min_dist_switch_value, 49, switch_min_dist ); cvCreateTrackbar( "dp", "Image", &dp_switch_value, 9, switch_dp ); cvCreateTrackbar( "High", "Canny", &high_switch_value, 499, switch_callback_h ); cvCreateTrackbar( "Low", "Canny", &low_switch_value, 499, switch_callback_l ); cvCreateTrackbar( "Threshold", "Threshold", &threshold_switch_value, 199, switch_threshold ); cvCreateTrackbar( "Max", "Threshold", &threshold_max_switch_value, 500, switch_threshold_max ); int N = 7; double dp = dpInt+1; double min_dist = min_distInt+1; double lowThresh = lowInt + 1; double highTresh = highInt + 1; double threshold = thresholdInt+1; double threshold_max = threshold_maxInt+1; cvThreshold(greyImg, contrastImg, threshold, threshold_max, CV_THRESH_TOZERO_INV); cvCanny(contrastImg, cannyImg, lowThresh*N*N, highTresh*N*N, N); // CvSeq* circles =cvHoughCircles(greyImg, storage, CV_HOUGH_GRADIENT, 35, 25); CvSeq* circles =cvHoughCircles(cannyImg, storage, CV_HOUGH_GRADIENT, dp, min_dist); // dp is image resolution // min_dist is the minimum distance between circles for (int i = 0; i < (circles ? circles->total : 0); i++) { float* p = (float*)cvGetSeqElem( circles, i ); cvCircle( drawnImg, cvPoint(cvRound(p[0]),cvRound(p[1])),3, CV_RGB(0,255,0), -1, 8, 0 ); } cvShowImage("Image", drawnImg); cvShowImage("Canny", cannyImg); cvShowImage("Threshold", contrastImg); char b; while (b != 98) { b = cvWaitKey(1); } b=0; } }
int hue_circles_detector(const sensor_msgs::ImageConstPtr& InputImage, std::vector<Circle> &myCircles) { IplImage *myImage = NULL; cv::Mat inputImage; cv::Mat hsvImage; cv::Mat hueImage; cv::Mat satImage; cv::Mat binaryImage; cv::Mat outputImage; sensor_msgs::CvBridge bridge; try { myImage = bridge.imgMsgToCv(InputImage, "bgr8"); } catch (sensor_msgs::CvBridgeException& e) { ROS_ERROR("Could not convert from '%s' to 'bgr8'.", InputImage->encoding.c_str()); return -1; } cvNamedWindow("Input to hue circles detector"); cvNamedWindow("Circles"); cvNamedWindow("Working image"); cvStartWindowThread(); cvShowImage("Input to hue circles detector", myImage); IplImage* CirclesImage = cvCloneImage(myImage); // just used to display our found circles // create memory storage that will contain all the dynamic data CvMemStorage* storage = 0; storage = cvCreateMemStorage(0); // Convert IplImage to cv::Mat inputImage = cv::Mat (myImage).clone (); // output = input outputImage = inputImage.clone (); // Convert Input image from BGR to HSV cv::cvtColor (inputImage, hsvImage, CV_BGR2HSV); // Zero Matrices hueImage = cv::Mat::zeros(hsvImage.rows, hsvImage.cols, CV_8U); satImage = cv::Mat::zeros(hsvImage.rows, hsvImage.cols, CV_8U); binaryImage = cv::Mat::zeros(hsvImage.rows, hsvImage.cols, CV_8U); // HSV Channel 0 -> hueImage & HSV Channel 1 -> satImage int from_to[] = { 0,0, 1,1}; cv::Mat img_split[] = { hueImage, satImage}; cv::mixChannels(&hsvImage, 3,img_split,2,from_to,2); // **************************************************** // NOTE that i is ROWS and j is COLS // This is not x,y, it is rows and cols // 0,0 is upper left; 0, max is upper right; max, 0 is lower left; max,max is lower right // **************************************************** for(int i = 0; i < outputImage.rows; i++) { for(int j = 0; j < outputImage.cols; j++) { // The output pixel is white if the input pixel // hue is green and saturation is reasonable // in low light the tennis ball has a hue around 160 - 180, saturations around 30 to 40 // in normal light it has a hue around 20 - 40, saturations around 80 - 150 // in very high light, the tennis ball center goes full white, so saturation drops to <10 // in general, the background doesn't seem to come up too much with a very loose constraint // on saturation, so it works to allow mostly any saturation value. if( (hueImage.at<uchar>(i,j) > 20 && hueImage.at<uchar>(i,j) < 40 && satImage.at<uchar>(i,j) > 5)) // || (hueImage.at<uchar>(i,j) > 160 && hueImage.at<uchar>(i,j) < 180 && satImage.at<uchar>(i,j) > 20 )) binaryImage.at<uchar>(i,j) = 255; else { binaryImage.at<uchar>(i,j) = 0; // Clear pixel blue output channel outputImage.at<uchar>(i,j*3+0) = 0; // Clear pixel green output channel outputImage.at<uchar>(i,j*3+1) = 0; // Clear pixel red output channel outputImage.at<uchar>(i,j*3+2) = 0; } } } cv::Size strel_size; strel_size.width = 3; strel_size.height = 3; cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE,strel_size); cv::morphologyEx(binaryImage, binaryImage,cv::MORPH_OPEN,strel,cv::Point(-1, -1),3); // Convert White on Black to Black on White by inverting the image cv::bitwise_not(binaryImage,binaryImage); // Blur the image to improve detection cv::GaussianBlur(binaryImage, binaryImage, cv::Size(7, 7), 2, 2 ); cv::imshow ("Input to hue circles detector", inputImage); // Display Binary Image cv::imshow ("Working image", binaryImage); // Display segmented image //cv::imshow ("Circles", outputImage); //start drawing all the circles IplImage InputToHough = binaryImage; CvSeq* circles = cvHoughCircles( &InputToHough, storage, CV_HOUGH_GRADIENT, 1, 70, 140, 15, 20, 400); //cvHoughCircles( &InputToHough, storage, CV_HOUGH_GRADIENT, 2, InputToHough.height/50, MIN_RADIUS, MAX_RADIUS ); //output all the circles detected int NumCircles = circles->total; //cout << "\n\nFound " << NumCircles << " circles" << endl; if (NumCircles > MAX_CIRCLES) NumCircles = MAX_CIRCLES + 1; // so we don't print out too many for( int i = 0; i < NumCircles; i++ ){ float* p = (float*)cvGetSeqElem( circles, i ); //cout << "x = " << p[0] << ", y = " << p[1] // << ", radius = " << p[2] << endl; cvCircle(CirclesImage, cvPoint(cvRound(p[0]), cvRound(p[1])), cvRound(p[2]), CV_RGB(0,255,0), 2, 8, 0 ); Circle myLocalCircle; int myColor = 0, actualThreshold = 100; myLocalCircle.setValues(cvRound(p[0]), cvRound(p[1]), cvRound(p[2]), myColor, actualThreshold); myCircles.push_back(myLocalCircle); } // ends for NumCircles cvShowImage("Circles", CirclesImage); return NumCircles; }
////////////////////// // // 원 검출 // ////////////////////// void ColorTracking::draw_circle(IplImage* image) { CvSeq* m_circle = NULL; // 원 정보 CvMemStorage* storage1 = NULL; // 메모리 할당 //검출된 원을 위한 메모리 공간 할당 storage1 = cvCreateMemStorage(0); //원 갯수 저장 변수 circle_cnt = 0; //원의 중심점을 검출하기 위한 누산기의 해상도 // 1이면 입력영상과 같은 크기, 2이면 입력 영상의 가로/세로의 반크기의 누산기 double dp = 1.5; double min_dist = 300; //검출된 원의 중심 사이의 최소거리.작을 수록 많은 원이 검출 됨-? double cannyThreshold = 100; //cvCanny 함수 임계값 double accThreshold = 50; //cvCanny 함수 축적평면의 임계값 int min_radius = 50; //최소 반지름 int max_radius = 150; //최대 반지름 int cx, cy = 0; //소스영상, 메모리스토리지 포인터, 메소드인자, 영상의 해상도, 인접한 두 원사이의 최소거리 m_circle = cvHoughCircles(image, storage1, CV_HOUGH_GRADIENT, dp,min_dist, cannyThreshold, accThreshold, min_radius, max_radius); //원이 1개 라도 있으면 if(m_circle->total >0 ) { // 데이터를 내보내기 위한 전역 선언한 시퀸스로 복사 circles = cvCloneSeq(m_circle, storage0); //원 그리기 for(int k = 0; k < m_circle->total; k++) { float* circle; int radius; //검출된 원을 저장한 circles에서 원의 파라미터를 circle에 저장 //원의 중심 좌표 및 반지름이 배열에 순서대로 저장됨 circle = (float*)cvGetSeqElem(m_circle, k); cx = cvRound(circle[0]); //중심점 x 좌표 cy = cvRound(circle[1]); //중심점 y 좌표 radius = cvRound(circle[2]); //반지름 //원그리기 if(radius > min_radius && radius < max_radius) { //중심점 cvCircle(m_orig_img, cvPoint(cx, cy), 3, CV_RGB(240,0,255), -1, 8, 0); //검출라인 cvCircle(m_orig_img, cvPoint(cx, cy), radius, CV_RGB(255,255,255), 3, 8, 0); } } } else // 원이 없으면 { circles = NULL; circle_cnt = 0; } cvReleaseMemStorage(&storage1); }
int snaps_nav(struct snaps_nav_state_struct* nav_state, time_t *ptr_framestamptime){ //Get frame from camera for (int i=0; i<6; i++){ img = cvQueryFrame( capture ); if ( !img ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } } timestamp_frame(ptr_framestamptime); //Crop Image code cvSetImageROI(img,cvRect(1,1,540,380)); cvCopy(img, Cropped, NULL); //Change the color format from BGR to HSV cvCvtColor(Cropped, imgHSV, CV_BGR2HSV); //copy original img to be displayed in drawn Targets/DM img cvCopy(Cropped, TimgDrawn, NULL ); cvInRangeS(imgHSV, cvScalar(T_range_low,0,0,0), cvScalar(T_range_high,255,255,0), TargetsFilter); cvInRangeS(imgHSV, cvScalar(DM_range_low,0,0,0), cvScalar(DM_range_high,255,255,0), DMFilter); //Magenta Marker Image Processing cvErode(TargetsFilter, TargetsFilter, 0, 1); cvDilate(TargetsFilter, TargetsFilter, NULL, 1); //Dilate image cvSmooth(TargetsFilter, TargetsFilter, CV_GAUSSIAN, 3, 0, 0.0, 0.0); //Smooth Target image*/ //Orange Target Image Processing cvErode(DMFilter, DMFilter, 0, 1); cvDilate(DMFilter, DMFilter, NULL, 1); //Dilate image //cvSmooth(DMFilter, DMFilter, CV_GAUSSIAN, 3, 0, 0.0, 0.0); //Smooth DM image //Show filtered Images cvShowImage("TargetsFilter", TargetsFilter); //Show Targets filter image cvShowImage("DMFilter", DMFilter); //Show DM filter image //Show Noise Filter //Perform Canny on Images cvCanny(TargetsFilter, TimgCanny, T_canny_low, T_canny_high, 3); // Apply canny filter to the targets image cvCanny(DMFilter, DMimgCanny, DM_canny_low, DM_canny_high, 3); // Apply canny filter to the DM image cvShowImage("TCannyImage", TimgCanny); cvShowImage("DMCannyImage", DMimgCanny); // Find and Draw circles for the Targets image CvPoint Tpt; CvSeq* TimgHCirc = cvHoughCircles( TimgCanny, TcircStorage, CV_HOUGH_GRADIENT, // in, out, method, 2, //precision of the accumulator (2x the input image) T_rad_max*4, //min dist between circles T_tol_max, T_tol_min, //parm1, parm2 T_rad_min, T_rad_max); //min radius, max radius for (int i = 0; i < TimgHCirc->total; i++) { float* p = (float*) cvGetSeqElem(TimgHCirc, i); // To get the circle coordinates CvPoint pt = cvPoint(cvRound(p[0]), cvRound(p[1])); // Draw center of circles in green cvCircle(TimgDrawn, pt, 1, CV_RGB(0,255,0), -1, 8, 0 ); cvCircle(TimgDrawn, pt, cvRound(p[2]), CV_RGB(255,255,0), 2, 8, 0); // img, center, radius, color, thickness, line type, shift Tpt = cvPoint(cvRound(p[0]), cvRound(p[1])); if (i == 0){ Tpt = cvPoint(cvRound(p[0]), cvRound(p[1])); printf("Magenta Marker (x,y) - (%d, %d) \n", Tpt.x, Tpt.y); } else {printf("TM - There is an extra point frame not good"); } } // end of for // Find and Draw circles for the DM image CvPoint DMpt; CvSeq* DMimgHCirc = cvHoughCircles( DMimgCanny, DMcircStorage, CV_HOUGH_GRADIENT, // in, out, method, 2, //precision of the accumulator (2x the input image) DM_rad_max*4, //min dist between circles DM_tol_max, DM_tol_min, //parm1, parm2 DM_rad_min, DM_rad_max); //min radius, max radius for (int i=0; i<DMimgHCirc->total; i++) { float* p = (float*) cvGetSeqElem(DMimgHCirc, i); CvPoint pt = cvPoint(cvRound(p[0]), cvRound(p[1])); // Draw center of circles in green cvCircle(TimgDrawn, pt, 1, CV_RGB(255,0,0), -1, 8, 0 ); cvCircle(TimgDrawn, pt, cvRound(p[2]), CV_RGB(255,127,0), 2, 8, 0); // img, center, radius, color, thickness, line type, shift if (i == 0){ DMpt = cvPoint(cvRound(p[0]), cvRound(p[1])); printf("Red Marker(x,y) - (%d, %d)\n", DMpt.x, DMpt.y); } else {printf("DM - There is an extra point frame not good"); } } // end of for //Draw line in between points cvLine(TimgDrawn, Tpt, DMpt, CV_RGB(0,255,0), 1, 8, 0); d = sqrt(pow(Tpt.x-DMpt.x, 2)+pow(Tpt.y-DMpt.y, 2)); //distance in between points printf("Distance in between tagets %d \n", d); //Magenta target coordinates int MT_pt_x = Tpt.x; int MT_pt_y = Tpt.y; //Orange target coordinates int OT_pt_x = DMpt.x; int OT_pt_y = DMpt.y; //Minimum of the two coordinates int x_min; int x_max; int y_min; int y_max; if (MT_pt_x > OT_pt_x){ x_min = OT_pt_x; x_max = MT_pt_x; } else{ x_min = MT_pt_x; x_max = OT_pt_x; } //printf("x_min %d \n", x_min); //printf("x_max %d \n", x_max); if (MT_pt_y > OT_pt_y){ y_min = OT_pt_y; y_max = MT_pt_y; } else{ y_min = MT_pt_y; y_max = OT_pt_y; } //printf("y_min %d", y_min); //printf("y_max %d", y_max); //Center of targets point (CT) int CT_pt_x = (((x_max - x_min)/2) + x_min); int CT_pt_y = (((y_max - y_min)/2) + y_min); printf("Center coordinate (x, y) - (%d, %d) \n", CT_pt_x, CT_pt_y); //Draw halfway targets point CvPoint CT_pt = cvPoint(cvRound(CT_pt_x), cvRound(CT_pt_y)); cvCircle(img, CT_pt, 2, CV_RGB(255,0,0), -1, 8, 0); //Orientation int orientation_x = (OT_pt_x - CT_pt_x); int orientation_y = (CT_pt_y - OT_pt_y); double Theta = (((atan2(orientation_y, orientation_x )) * (180/3.14))+360); //if printf("Orientation %f Degrees \n", Theta); //cvResetImageROI(img); cvShowImage("TDrawnImage", TimgDrawn); //cvShowImage("DMDrawnImage", DMimgDrawn); //clear memory for target and DM circle finder //note: this may not be necessary cvClearMemStorage(TcircStorage); cvClearMemStorage(DMcircStorage); return 0; }
int main(int argc, char* argv[]) { int height,width,step,channels; //parameters of the image we are working on int i,j,k,t1min=0,t1max=0,t2min=0,t2max=0,t3min=0,t3max=0; // other variables used CvMat* threshold_matrix = cvCreateMat(2,3,CV_32FC1); CvFileStorage* temp = cvOpenFileStorage("threshold_matrix.xml",NULL,CV_STORAGE_READ); // Load the previous values of the threshold if they exist if(temp) { threshold_matrix = (CvMat*)cvLoad("threshold_matrix.xml"); t1min =(int) CV_MAT_ELEM(*threshold_matrix,float,0,0) ;t2min =(int) CV_MAT_ELEM(*threshold_matrix,float,0,1) ;t3min =(int) CV_MAT_ELEM(*threshold_matrix,float,0,2); t1max =(int) CV_MAT_ELEM(*threshold_matrix,float,1,0) ;t2max =(int) CV_MAT_ELEM(*threshold_matrix,float,1,1) ;t3max =(int) CV_MAT_ELEM(*threshold_matrix,float,1,2) ; } // Open capture device. 0 is /dev/video0, 1 is /dev/video1, etc. CvCapture* capture = cvCaptureFromCAM( 1 ); if( !capture ) { fprintf( stderr, "ERROR: capture is NULL \n" ); getchar(); return -1; } // grab an image from the capture IplImage* frame = cvQueryFrame( capture ); // Create a window in which the captured images will be presented cvNamedWindow( "Camera", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "F1", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "F2", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "F3", CV_WINDOW_AUTOSIZE ); //cvNamedWindow( "EdgeDetection", CV_WINDOW_AUTOSIZE ); /// Create Trackbars char TrackbarName1[50]="t1min"; char TrackbarName2[50]="t1max"; char TrackbarName3[50]="t2min"; char TrackbarName4[50]="t2max"; char TrackbarName5[50]="t3min"; char TrackbarName6[50]="t3max"; cvCreateTrackbar( TrackbarName1, "F1", &t1min, 260 , NULL ); cvCreateTrackbar( TrackbarName2, "F1", &t1max, 260, NULL ); cvCreateTrackbar( TrackbarName3, "F2", &t2min, 260 , NULL ); cvCreateTrackbar( TrackbarName4, "F2", &t2max, 260, NULL ); cvCreateTrackbar( TrackbarName5, "F3", &t3min, 260 , NULL ); cvCreateTrackbar( TrackbarName6, "F3", &t3max, 260, NULL ); // Load threshold from the slider bars in these 2 parameters CvScalar hsv_min = cvScalar(t1min, t2min, t3min, 0); CvScalar hsv_max = cvScalar(t1max, t2max ,t3max, 0); // get the image data height = frame->height; width = frame->width; step = frame->widthStep; // capture size - CvSize size = cvSize(width,height); // Initialize different images that are going to be used in the program IplImage* hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3); // image converted to HSV plane IplImage* thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1); // final thresholded image IplImage* thresholded1 = cvCreateImage(size, IPL_DEPTH_8U, 1); // Component image threshold IplImage* thresholded2 = cvCreateImage(size, IPL_DEPTH_8U, 1); IplImage* thresholded3 = cvCreateImage(size, IPL_DEPTH_8U, 1); IplImage* filtered = cvCreateImage(size, IPL_DEPTH_8U, 1); //smoothed image while( 1 ) { // Load threshold from the slider bars in these 2 parameters hsv_min = cvScalar(t1min, t2min, t3min, 0); hsv_max = cvScalar(t1max, t2max ,t3max, 0); // Get one frame frame = cvQueryFrame( capture ); if( !frame ) { fprintf( stderr, "ERROR: frame is null...\n" ); getchar(); break; } // Covert color space to HSV as it is much easier to filter colors in the HSV color-space. cvCvtColor(frame, hsv_frame, CV_BGR2HSV); // Filter out colors which are out of range. cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded); // the below lines of code is for visual purpose only remove after calibration //--------------FROM HERE----------------------------------- //Split image into its 3 one dimensional images cvSplit( hsv_frame,thresholded1, thresholded2, thresholded3, NULL ); // Filter out colors which are out of range. cvInRangeS(thresholded1,cvScalar(t1min,0,0,0) ,cvScalar(t1max,0,0,0) ,thresholded1); cvInRangeS(thresholded2,cvScalar(t2min,0,0,0) ,cvScalar(t2max,0,0,0) ,thresholded2); cvInRangeS(thresholded3,cvScalar(t3min,0,0,0) ,cvScalar(t3max,0,0,0) ,thresholded3); //-------------REMOVE OR COMMENT AFTER CALIBRATION TILL HERE ------------------ // Memory for hough circles CvMemStorage* storage = cvCreateMemStorage(0); // hough detector works better with some smoothing of the image cvSmooth( thresholded, thresholded, CV_GAUSSIAN, 9, 9 ); //hough transform to detect circle CvSeq* circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 2, thresholded->height/4, 100, 50, 10, 400); for (int i = 0; i < circles->total; i++) { //get the parameters of circles detected float* p = (float*)cvGetSeqElem( circles, i ); printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] ); // draw a circle with the centre and the radius obtained from the hough transform cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), //plot centre 2, CV_RGB(255,255,255),-1, 8, 0 ); cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])), //plot circle cvRound(p[2]), CV_RGB(0,255,0), 2, 8, 0 ); } /* for testing purpose you can show all the images but when done with calibration only show frame to keep the screen clean */ cvShowImage( "Camera", frame ); // Original stream with detected ball overlay cvShowImage( "HSV", hsv_frame); // Original stream in the HSV color space cvShowImage( "After Color Filtering", thresholded ); // The stream after color filtering cvShowImage( "F1", thresholded1 ); // individual filters cvShowImage( "F2", thresholded2 ); cvShowImage( "F3", thresholded3 ); //cvShowImage( "filtered", thresholded ); cvReleaseMemStorage(&storage); //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator if( (cvWaitKey(10) & 255) == 27 ) break; } //Save the threshold values before exiting *((float*)CV_MAT_ELEM_PTR( *threshold_matrix, 0, 0 ) ) = t1min ;*((float*)CV_MAT_ELEM_PTR( *threshold_matrix, 0, 1 ) ) = t2min ;*((float*)CV_MAT_ELEM_PTR( *threshold_matrix, 0, 2 ) ) = t3min ; *((float*)CV_MAT_ELEM_PTR( *threshold_matrix, 1, 0 ) ) = t1max ;*((float*)CV_MAT_ELEM_PTR( *threshold_matrix, 1, 1 ) ) = t2max ;*((float*)CV_MAT_ELEM_PTR( *threshold_matrix, 1, 2 ) ) = t3max ; cvSave("threshold_matrix.xml",threshold_matrix); // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyWindow( "mywindow" ); return 0; }