使用 OpenCV 查找二值图像中最大 Blob 的边界框的最有效方法是什么?不幸的是,OpenCV 没有特定的 Blob 检测功能。我应该只使用 findContours()
并在列表中搜索最大的吗?
最佳答案
这里。它。是。 (仅供引用:尽量不要偷懒,弄清楚下面我的函数中发生了什么。
cv::Mat findBiggestBlob(cv::Mat & matImage){
int largest_area=0;
int largest_contour_index=0;
vector< vector<Point> > contours; // Vector for storing contour
vector<Vec4i> hierarchy;
findContours( matImage, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); // Find the contours in the image
for( int i = 0; i< contours.size(); i++ ) {// iterate through each contour.
double a=contourArea( contours[i],false); // Find the area of contour
if(a>largest_area){
largest_area=a;
largest_contour_index=i; //Store the index of largest contour
//bounding_rect=boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
}
}
drawContours( matImage, contours, largest_contour_index, Scalar(255), CV_FILLED, 8, hierarchy ); // Draw the largest contour using previously stored index.
return matImage;
}
关于OpenCV - 在二值图像中找到最大 Blob 的边界框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16746473/