Ask Your Question
0

Displaying radius of a circle

asked 2013-09-11 03:07:33 -0500

Akoo gravatar image

updated 2013-09-11 04:11:03 -0500

Hi everybody, I am learning OpenCV now and I have wrote a simple program which displays the bounding circle of a contour. Now I would like to know, how can I display in the same image the radius of that circle. I leave an image where you can see the bounding circle of a contour and where I would like to draw the radius of that circle. Any help would be apreciated.

Thanks in advance. This is the code I've got:

void CirculoMin(int, void* ) { Mat thresh_output; vector<vector<point> > contornos; vector<vec4i> jerarquia;

  threshold( imgThresh,thresh_output, 50, 50, THRESH_TRUNC );
 findContours( thresh_output, contornos, jerarquia, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

vector<vector<Point> > contours_poly( contornos.size() );
vector<Rect> boundRect( contornos.size() );
vector<Point2f>centro( contornos.size() );
vector<float>radio( contornos.size() );

for( uint i = 0; i < contornos.size(); i++ ){

      approxPolyDP( Mat(contornos[i]), contours_poly[i], 3, true );
      minEnclosingCircle( (Mat)contours_poly[i], centro[i], radio[i] );

}

Mat dibujo = Mat::zeros( thresh_output.size(), CV_8UC3 );

for( uint i = 0; i< contornos.size(); i++ ){

   Scalar R = Scalar(0,0,255); /*color rojo*/
   Scalar G = Scalar(0,255,0);/*color verde*/
   Scalar B = Scalar(255,0,0);/*color azul*/

   drawContours( dibujo, contours_poly, i, G, 1, 8, vector<Vec4i>(), 0, Point() );

   if((int)radio[i] >= 15){        
       circle( dibujo, centro[i], (int)radio[i], R, 2, 8, 0 );        
   }//if

}//for

imshow( "Círculo mínimo", dibujo );

}

image description

edit retag flag offensive close merge delete

Comments

Post the code.

Spas Hristov gravatar imageSpas Hristov ( 2013-09-11 03:14:53 -0500 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2013-09-11 03:19:38 -0500

Moster gravatar image

updated 2013-09-11 04:17:54 -0500

So you really just want to draw the radius in that circle? I mean, to draw the circle, you already have the center and the radius itself. So you can just use cv::line() to draw the radius.

cv::line(img, center, cv::Point(center.x + radius, center.y), cv::Scalar(255, 255, 255, 255))


char buffer[256];
sprintf(buffer, "Radius: %f", radius);
cv::putText(img, std::string(buffer), cv::Point(0,32), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(255,255,255,255));
edit flag offensive delete link more

Comments

I know I've alredy got the radius and the center to draw that circle, but I don't know how to display it in the image.

Akoo gravatar imageAkoo ( 2013-09-11 04:02:54 -0500 )edit

The code I provided draws the radius in form of a line. Is that what you want?

Moster gravatar imageMoster ( 2013-09-11 04:06:53 -0500 )edit

I would like to show the radius in number, although I could show a line but also the number. I had tried with putText but in this function I only can show a constant string. I'll edit my post to upload my code.

Akoo gravatar imageAkoo ( 2013-09-11 04:10:26 -0500 )edit

I edited it to use putText.

Moster gravatar imageMoster ( 2013-09-11 04:18:42 -0500 )edit

Thank you very much Moster!!!

Akoo gravatar imageAkoo ( 2013-09-11 04:45:17 -0500 )edit

Another one more question, And what should I do if I have two circles in the image? It would be possible to show both radiu and discrimine it?

Akoo gravatar imageAkoo ( 2013-09-11 05:21:33 -0500 )edit

Yes, of course. Just run the cv::putText in a loop or something and make sure you change the position cv::Point(x, y)

Moster gravatar imageMoster ( 2013-09-11 05:25:17 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2013-09-11 03:07:33 -0500

Seen: 686 times

Last updated: Sep 11 '13