Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, basically you need to detect <strong>circles</strong>. Have you seen <code>cvHoughCircles()</code>? Are you allowed to use it?</p> <p>This page has good info on how <a href="http://cgi.cse.unsw.edu.au/~cs4411/wiki/index.php?title=OpenCV_Guide" rel="noreferrer">detecting stuff with OpenCV</a>. You might be more interested on <a href="http://cgi.cse.unsw.edu.au/~cs4411/wiki/index.php?title=OpenCV_Guide#Detecting_circles" rel="noreferrer">section 2.5</a>. </p> <p>This is a small demo I just wrote to detect coins in this picture. Hopefully you can use some part of the code to your advantage.</p> <p><strong>Input</strong>: <img src="https://i.stack.imgur.com/hoD2b.jpg" alt="input img"></p> <p><strong>Outputs</strong>: <img src="https://i.stack.imgur.com/Zfss9.png" alt="output opencv img"></p> <pre><code>// compiled with: g++ circles.cpp -o circles `pkg-config --cflags --libs opencv` #include &lt;stdio.h&gt; #include &lt;cv.h&gt; #include &lt;highgui.h&gt; #include &lt;math.h&gt; int main(int argc, char** argv) { IplImage* img = NULL; if ((img = cvLoadImage(argv[1]))== 0) { printf("cvLoadImage failed\n"); } IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1); CvMemStorage* storage = cvCreateMemStorage(0); cvCvtColor(img, gray, CV_BGR2GRAY); // This is done so as to prevent a lot of false circles from being detected cvSmooth(gray, gray, CV_GAUSSIAN, 7, 7); IplImage* canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1); IplImage* rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3); cvCanny(gray, canny, 50, 100, 3); CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, gray-&gt;height/3, 250, 100); cvCvtColor(canny, rgbcanny, CV_GRAY2BGR); for (size_t i = 0; i &lt; circles-&gt;total; i++) { // round the floats to an int float* p = (float*)cvGetSeqElem(circles, i); cv::Point center(cvRound(p[0]), cvRound(p[1])); int radius = cvRound(p[2]); // draw the circle center cvCircle(rgbcanny, center, 3, CV_RGB(0,255,0), -1, 8, 0 ); // draw the circle outline cvCircle(rgbcanny, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 ); printf("x: %d y: %d r: %d\n",center.x,center.y, radius); } cvNamedWindow("circles", 1); cvShowImage("circles", rgbcanny); cvSaveImage("out.png", rgbcanny); cvWaitKey(0); return 0; } </code></pre> <p>The detection of the circles depend a lot on the parameters of <code>cvHoughCircles()</code>. Note that in this demo I used Canny as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload