# Displaying radius of a circle 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() );


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

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


}

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() );

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


}//for

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

} edit retag close merge delete

Sort by » oldest newest most voted

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;
cv::putText(img, std::string(buffer), cv::Point(0,32), cv::FONT_HERSHEY_PLAIN, 1, cv::Scalar(255,255,255,255));

more

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.

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

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.

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?

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

Official site

GitHub

Wiki

Documentation