Debugging HoughCircles code to detect Yellow circles in an image [closed]

asked 2019-06-25 13:47:00 -0500

updated 2019-07-01 22:20:07 -0500

supra56 gravatar image

In my code, I'm trying to threshold an image to Yellow hues and then select and color those Yellow circles. What am I missing in my code? Because although I see the image after Gaussian to avoid false circle detection, the HoughCircles isn't picking and outlining the Yellow circles? Please let me know what information I can share to debug. Code:

 * YellowCircleDetect.cpp
 *  Created on: Jun 13, 2019
 *      Author: techsri
#include "YellowcircleDetect.h"

using namespace cv;
using namespace std;

// we'll be using the HSV colour space, instead of the more common RGB colour space. In HSV, each "tint" of colour is assigned a particular number (the Hue).
//The "amount" of colour is assigned another number (the Saturation) and the brightness of the colour is assigned another number (the Intensity or Value).
//This gives us the advantage of having a single number (hue) for the yellow circle.

IplImage* get_thresholded_image(IplImage* img)
        // Convert the image into an HSV image
        IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
        cvCvtColor(img, imgHSV, CV_BGR2HSV);
        IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);
        cvInRangeS(imgHSV, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed);

        return imgThreshed;

    /** @function main */
int main(int argc, char** argv)
      Mat src;

      /// Read the image
      const char* filename = argc >=2 ? argv[1] : "./data/computer-vision-test-image.png";
         // Loads an image
      src = imread( filename, IMREAD_COLOR );
         // Check if image is loaded fine
             printf(" Error opening image\n");
             printf(" Program Arguments: [image_name -- default %s] \n", filename);
             return -1;
      //౩ Channels conversion of mat to IpIImage.
      IplImage* ptr_image = cvCreateImage(cvSize(src.cols, src.rows), 8, 3);
      IplImage ipltemp = src;
      cvCopy(&ipltemp, ptr_image);

      //Threshold to yellow hue. Yellow becomes white. Rest becomes blackened.
     IplImage*  img_yellow_thresh =  get_thresholded_image(ptr_image);

     //Convert the thresholded image to an cv::Mat structure for use in HoughCircles function.
     cv::Mat src_gray = cv::cvarrToMat(img_yellow_thresh);

     // Reduce the noise so we avoid false circle detection
     GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );

     cvShowImage("Noise reduced grey Image",img_yellow_thresh);
     std::vector<cv::Vec3f> circles;
      // Apply the Hough Transform to find just the circles
     //gray: Input image (grayscale).
     //HOUGH_GRADIENT: Define the detection method. Currently this is the only one available in OpenCV.
     //dp = 1: The inverse ratio of resolution.
     //min_dist = gray.rows/16: Minimum distance between detected centers.
     //param_1 = 200: Upper threshold for the internal Canny edge detector.
     //param_2 = 100*: Threshold for center detection.
     //min_radius = 0: Minimum radius to be detected. If unknown, put zero as default.
     //max_radius = 0: Maximum radius to be detected. If unknown, put zero asstll default.
     HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/16, 200, 100, 0, 0 );
     imshow("Noise reduced grey Image",src_gray);

     for( size_t i = 0; i < circles.size(); i++ )
         Vec3i c = circles[i];
         Point center = Point(c[0], c[1]);
         // circle center
         circle( src, center, 1, Scalar(0,100,100), 3, LINE_AA);
         // circle outline
         int radius = c[2];
         circle( src, center, radius, Scalar(255,0,255), 3, LINE_AA);

     imshow("detected circles", src);

     return 0;

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by berak
close date 2019-06-25 14:12:20.932321



IplImage* is no more used since 2010

berak gravatar imageberak ( 2019-06-25 14:13:07 -0500 )edit

don't debug it. burn it.

(then rewrite using cv::Mat and start all over, please)

more recent docs

berak gravatar imageberak ( 2019-06-25 14:14:38 -0500 )edit

Thanks. I will

opencvsri gravatar imageopencvsri ( 2019-06-25 16:07:06 -0500 )edit