OpenCV - 在二值图像中找到最大 Blob 的边界框

标签 opencv image-processing feature-detection

使用 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/

相关文章:

python - OpenCV putText() 换行符

python - 轮廓检测 : reduce glare on image opencv/python

opencv - C 中的 SIFT 特征检测

python - 在 Python 的 OpenCV 中找不到 cv2.cv 模块

python - 无法使用pip3安装OpenCV

python - 模块 'cv2.cv2' 没有属性 'cv'

ios - iOS 中的照相亭。使用 OpenCV 还是 OpenGL ES?

android - android中的自由手字符识别

java - OpenCV:在 Java 中使用单应性和透视变换将对象拟合到场景中

c++ - 使用 OpenCV 改进特征点的匹配