Halcon source code:

Read_image (Image1, 'F:/ future project/tube /FindTube/1.jpg') rGB1_to_gray (Image1, GrayImage) threshold (GrayImage, Regions, 43, 111) connection (Regions, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 666) select_Shape (SelectedRegions, SelectedRegions1, 'circularity', 'and', 0.45, 1)Copy the code

Of course Halcon did a lot of work behind the scenes.

The most important thing in a few lines of code is that the operator is “select_Shape”. This operator has a lot of arguments, so I’m familiar with those two.

If I wanted this in Opencv, I’d have to try it out myself. I used similar function names as a tribute to the implementation.

// selectShape. CPP: select contours // by: jsxyhelu(1755311380) #include "stdafx.h" #include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using namespace std; using namespace cv; #define VP vector<Point> RNG RNG (12345); // Threshold void threshold2(Mat gray,Mat& thresh,int minvalue,int maxValue) {Mat threshold2; Mat thresh2; Threshold (gray, thresh1, 43255, THRESH_BINARY); Threshold (gray, thresh2, 111255, THRESH_BINARY_INV); thresh = thresh1 & thresh2; } vector<VP> connection2(Mat SRC,Mat& draw) {draw = Mat::zeros(src.rows,src.cols,CV_8UC3); vector<VP>contours; findContours(src,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE); for (int i=0; i<contours.size(); I++) {Scalar color = red (RNG. Uniform (0,255), RNG. Uniform (0,255), RNG. drawContours(draw,contours,i,color,-1); } return contours; } //select_shape vector<VP> selectShapeArea(Mat src,Mat& draw,vector<VP> contours,int minvalue,int maxvalue) { vector<VP> result_contours; draw = Mat::zeros(src.rows,src.cols,CV_8UC3); for (int i=0; i<contours.size(); i++) { int countour_area = contourArea(contours[i]); if (countour_area >minvalue && countour_area<maxvalue) { result_contours.push_back(contours[i]); } } for (int i=0; i<result_contours.size(); I++) {Scalar color = red (RNG. Uniform (0,255), RNG. Uniform (0,255), RNG. drawContours(draw,result_contours,i,color,-1); } return result_contours; } // Float calculateCircularity(VP contour) {Point2f center; float radius = 0; minEnclosingCircle((Mat)contour,center,radius); Float fsum = 0; float fsum = 0; float fsum = 0; float fcompare = 0; for (int i=0; i<contour.size(); i++) { Point2f ptmp = contour[i]; float fdistenct = sqrt((float)((ptmp.x - center.x)*(ptmp.x - center.x)+(ptmp.y - center.y)*(ptmp.y-center.y))); float fdiff = abs(fdistenct - radius); fsum = fsum + fdiff; } fcompare = fsum/(float)contour.size(); return fcompare; } //select_shape vector<VP> selectShapeCircularity(Mat src,Mat& draw,vector<VP> contours,float minvalue,float maxvalue) { vector<VP> result_contours; draw = Mat::zeros(src.rows,src.cols,CV_8UC3); for (int i=0; i<contours.size(); i++) { float fcompare = calculateCircularity(contours[i]); if (fcompare >=minvalue && fcompare <=maxvalue) { result_contours.push_back(contours[i]); } } for (int i=0; i<result_contours.size(); I++) {Scalar color = red (RNG. Uniform (0,255), RNG. Uniform (0,255), RNG. drawContours(draw,result_contours,i,color,-1); } return result_contours; } int _tmain(int argc, _TCHAR* argv[]) { Mat src; Mat gray; Mat thresh; Mat draw_connection; Mat draw_area; Mat draw_circle; vector<VP>contours_connection; vector<VP>contours_area; vector<VP>contours_circle; vector<VP>contours_tmp; //read_image (Image1, 'F:/ future project /FindTube/FindTube/1.jpg') SRC = imread("1.jpg"); //rgb1_to_gray (Image1, GrayImage) cvtColor(src,gray,COLOR_BGR2GRAY); Threshold2 (Gray, Thresh,43,111); // Threshold (GrayImage, Regions, 43,111) threshold2(Gray, Thresh,43,111); //connection (Regions, ConnectedRegions) contours_connection = connection2(thresh.clone(),draw_connection); //select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 666) contours_area = selectShapeArea (thresh. Clone (), draw_area, contours_connection, 150666); // select_Shape (SelectedRegions, SelectedRegions1, 'circularity', 'and', 0.45, 1) contours_circle = selectShapeCircularity (thresh. Clone (), draw_circle, contours_area, 1, 6); Imshow (" SRC ", SRC); imshow("thresh",thresh); imshow("draw_connection",draw_connection); imshow("draw_area",draw_area); imshow("draw_circle",draw_circle); waitKey(); }Copy the code

 

reference

Select Select_Shape

www.cnblogs.com/jsxyhelu/p/…

Opencv’s practical research – analyzing contours and finding boundary points

www.cnblogs.com/jsxyhelu/p/…