直线检测
void LineDetect ( const cv:: Mat & binaryImage)
{
cv:: Mat xImage, yImage, binaryImage1, binaryImage2;
cv:: Mat element = cv:: getStructuringElement ( cv:: MORPH_RECT, cv:: Size ( 15 , 3 ) ) ;
morphologyEx ( binaryImage, binaryImage1, cv:: MORPH_CLOSE, element) ;
cv:: imwrite ( "close1.jpg" , binaryImage1) ;
cv:: Sobel ( binaryImage1, xImage, CV_8U, 1 , 0 ) ;
element = cv:: getStructuringElement ( cv:: MORPH_RECT, cv:: Size ( 3 , 15 ) ) ;
morphologyEx ( binaryImage1, binaryImage2, cv:: MORPH_CLOSE, element) ;
cv:: imwrite ( "close2.jpg" , binaryImage2) ;
cv:: Sobel ( binaryImage2, yImage, CV_8U, 0 , 1 ) ;
vector< cv:: Vec4i> lines;
cv:: HoughLinesP ( xImage, lines, 1 , CV_PI / 180 , 10 , 100 , 50 ) ;
for ( size_t i = 0 ; i < lines. size ( ) ; i++ )
{
cv:: Vec4i l = lines[ i] ;
line ( xImage, cv:: Point ( l[ 0 ] , l[ 1 ] ) , cv:: Point ( l[ 2 ] , l[ 3 ] ) , cv:: Scalar ( 255 ) , 3 , cv:: LINE_AA) ;
}
cv:: imwrite ( "xImage.jpg" , xImage) ;
cv:: imwrite ( "yImage.jpg" , yImage) ;
}
轮廓检测
void ContourDetect ( const cv:: Mat & binaryImage, vector< vector< cv:: Point>> & contours)
{
int closeSize = 25 ;
float areaRatio = 0.005 ;
int imgArea = binaryImage. cols * binaryImage. rows;
cv:: Mat closeImg;
cv:: Mat element = cv:: getStructuringElement ( cv:: MORPH_RECT, cv:: Size ( closeSize, closeSize) ) ;
morphologyEx ( binaryImage, closeImg, cv:: MORPH_CLOSE, element) ;
cv:: imwrite ( "closeImg.jpg" , closeImg) ;
cv:: Mat edges_image;
Canny ( closeImg, edges_image, 100 , 200 ) ;
cv:: imwrite ( "edges_image.jpg" , edges_image) ;
vector< vector< cv:: Point>> allContours;
cv:: findContours ( edges_image, allContours, cv:: RETR_EXTERNAL, cv:: CHAIN_APPROX_SIMPLE) ;
cout << "allContours size is: " << allContours. size ( ) << endl;
for ( auto contour: allContours)
{
cv:: Rect rect = cv:: boundingRect ( contour) ;
if ( rect. area ( ) > imgArea * areaRatio)
{
contours. push_back ( contour) ;
}
}
cv:: Mat contourImg = cv:: Mat :: zeros ( binaryImage. size ( ) , binaryImage. type ( ) ) ;
cout << "contours size is: " << contours. size ( ) << endl;
for ( int i = 0 ; i < contours. size ( ) ; i++ )
{
cv:: drawContours ( contourImg, contours, i, cv:: Scalar ( 255 ) , 2 ) ;
}
cv:: imwrite ( "contours.jpg" , contourImg) ;
}
连通区域检测
void ConnectedComponentRect ( const cv:: Mat & binaryImage, vector< cv:: Rect> & rects)
{
int closeSize = 25 ;
float areaRatio = 0.001 ;
int imgArea = binaryImage. cols * binaryImage. rows;
cv:: Mat closeImg;
cv:: Mat element = cv:: getStructuringElement ( cv:: MORPH_RECT, cv:: Size ( closeSize, closeSize) ) ;
morphologyEx ( binaryImage, closeImg, cv:: MORPH_CLOSE, element) ;
cv:: imwrite ( "closeImg.jpg" , closeImg) ;
cv:: Mat labels, stats, centroids;
int num_objects = cv:: connectedComponentsWithStats ( closeImg, labels, stats, centroids) ;
for ( int i = 1 ; i < num_objects; i++ ) {
int * stat = stats. ptr < int > ( i) ;
int left = stat[ cv:: CC_STAT_LEFT] ;
int top = stat[ cv:: CC_STAT_TOP] ;
int width = stat[ cv:: CC_STAT_WIDTH] ;
int height = stat[ cv:: CC_STAT_HEIGHT] ;
int area = stat[ cv:: CC_STAT_AREA] ;
if ( area > imgArea * areaRatio)
{
rects. push_back ( cv:: Rect ( left, top, width, height) ) ;
}
}
cout << num_objects << " rect size is:" << rects. size ( ) << endl;
}