代码
# include <iostream>
# include <opencv.hpp> int main ( )
{ std:: string imgPath ( "D:\\prostate_run\\result_US_20230804_141531\\mask\\us\\104.bmp" ) ; cv:: Mat imgGray = cv:: imread ( imgPath, 0 ) ; cv:: Mat kernel = cv:: getStructuringElement ( cv:: MORPH_RECT, cv:: Size ( 5 , 5 ) ) ; cv:: morphologyEx ( imgGray, imgGray, cv:: MORPH_OPEN, kernel) ; cv:: Mat binary; cv:: threshold ( imgGray, binary, 0 , 255 , cv:: THRESH_BINARY | cv:: THRESH_OTSU) ; std:: vector< std:: vector< cv:: Point>> contours; std:: vector< cv:: Vec4i> hierachy; cv:: findContours ( binary, contours, hierachy, cv:: RETR_EXTERNAL, cv:: CHAIN_APPROX_SIMPLE) ; std:: cout << contours. size ( ) << std:: endl; cv:: Mat contourImage = cv:: Mat :: zeros ( imgGray. size ( ) , CV_8UC3) ; cv:: Scalar redColor = cv:: Scalar ( 0 , 0 , 255 ) ; cv:: drawContours ( contourImage, contours, 0 , redColor, 1 , cv:: LINE_8) ; std:: cout << contours[ 0 ] . size ( ) << std:: endl; cv:: Mat contourImage2 = cv:: Mat :: zeros ( imgGray. size ( ) , CV_8UC3) ; for ( int i = 0 ; i < contours[ 0 ] . size ( ) ; ++ i) { cv:: Point point = contours[ 0 ] [ i] ; cv:: circle ( contourImage2, point, 1 , redColor, - 1 ) ; } cv:: imshow ( "w2" , contourImage2) ; cv:: waitKey ( ) ; return 0 ;
}