Home > Software engineering >  Using opencv detection circle can identify all round but center can only output one
Using opencv detection circle can identify all round but center can only output one

Time:05-11

Mat src_img, binary_img dst_img;
Int main ()
{
Src_img=imread (" D: \ \ LessonWork \ \ yuan \ \ 1. PNG ", IMREAD_GRAYSCALE);
/read/gray, in read in the image of color conversion at the same time, it can improve the speed and less memory usage
If (src_img. Empty ())
{
Printf (" could not load the image... \n");
return -1;
}

Imshow (" original, "src_img);

//image binarization
Threshold (src_img binary_img, 0, 255, THRESH_BINARY | THRESH_OTSU);
Imshow (" binary image, "binary_img);

//morphology operations
Mat kernel=getStructuringElement (MORPH_RECT, Size (5, 5), Point (1, 1));//to build the structure of the morphological operations yuan
MorphologyEx (binary_img dst_img, MORPH_CLOSE, kernel, Point (1, 1));//closed operations
Imshow (" closed operation ", dst_img);

The kernel=getStructuringElement (MORPH_RECT, Size (5, 5), Point (1, 1));//to build the structure of the morphological operations yuan
MorphologyEx (dst_img dst_img, MORPH_OPEN, kernel, Point (1, 1));//operation
Imshow (" open "operation, dst_img);

//looking for contour
Vector Contours.
VectorFindContours (dst_img, contours, hireachy RETR_TREE, CHAIN_APPROX_SIMPLE, Point ());

Mat result_img=Mat: : zeros (src_img. The size (), CV_8UC3);//create and the size of the original image with black background
Point circle_center;//define the center coordinates,
For (auto t=0; T & lt; Contours. The size (); + + t)
{
//area of filter
Double area=contourArea (contours [t]);//calculation point set by the area of the surrounding area
If (area & lt; 100)//tan to select the outline of contour area is greater than 100
continue;
//transverse and longitudinal than filtering
The Rect the Rect=boundingRect (contours [t]);//o point set, the minimum vertical outsourcing rectangular
Float thewire=float (the rect. Width)/float (the rect. Height);//to find the aspect ratio

If (thewire & lt; 1.1 & amp; & Thewire & gt; 0.9)//because round external vertical rectangle must be similar to a square, so close to the aspect ratio of 1.0
{
DrawContours (result_img, contours, t, Scalar (0, 0, 255), and 1, 8, Mat (), 0, Point ());//draw a circle on the black background map
Printf (" the area of a circle: % f \ n ", area);
Double arc_length=arcLength (contours [t], true);//around the perimeter of the area by calculating point set
Printf (" the perimeter of a circle: % f \ n ", arc_length);
Int x=the rect. X + the rect. Width/2;
Int y=the rect. Y + the rect. Height/2;
Circle_center=Point (x, y);//get the center of a circle coordinate
Cout & lt;
Circle (result_img, circle_center, 2, Scalar (0, 255, 255), 2, 8, 0).
}
}
Imshow (" result ", result_img);

Mat circle_img=src_img. Clone ();
CvtColor (circle_img circle_img, COLOR_GRAY2BGR);//greyscale image into a color chart
Circle (circle_img, circle_center, 2, Scalar (0, 0, 255), 2, 8, 0).//draw a circle on the original
Imshow (" result ", circle_img);

waitKey(0);
return 0;
}




Why this can only draw a circle on the original ah, I just learn this
  • Related