如何在二值图像上计算白色对象的数量?

4
我正在尝试从图像中计算对象。我使用对数照片,并采用一些步骤来获取二进制图像。 在此输入图片描述 这是我的代码:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <features2d.hpp>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
    //load image
    Mat img = imread("kayu.jpg", CV_LOAD_IMAGE_COLOR);
    if(img.empty())
       return -1;
    //namedWindow( "kayu", CV_WINDOW_AUTOSIZE );
    imshow("kayu", img);

    //convert to b/w
    Mat bw;
    cvtColor(img, bw, CV_BGR2GRAY);
    imshow("bw1", bw);

    threshold(bw, bw, 40, 255, CV_THRESH_BINARY);
    imshow("bw", bw);

    //distance transform & normalisasi
    Mat dist;
    distanceTransform(bw, dist, CV_DIST_L2, 3);
    normalize(dist, dist, 0, 2., NORM_MINMAX);
    imshow("dist", dist);

    //threshold to draw line
    threshold(dist, dist, .5, 1., CV_THRESH_BINARY);
    imshow("dist2", dist);

    //dist = bw;
    //dilasi
    Mat dilation, erotion, element;
    int dilation_type = MORPH_ELLIPSE;
    int dilation_size = 17;

    element = getStructuringElement(dilation_type, Size(2*dilation_size + 1, 2*dilation_size+1), Point(dilation_size, dilation_size ));
    erode(dist, erotion, element);
    int erotionCount = 0;
    for(int i=0; i<erotionCount; i++){
        erode(erotion, erotion, element);
    }
    imshow("erotion", erotion);

    dilate(erotion, dilation, element);
    imshow("dilation", dilation);
    waitKey(0);
    return 0;
}

如您所见,我使用腐蚀和膨胀来得到更好的原木圆形对象。我的问题是,我卡在了计数对象上。我尝试了SimpleBlobDetector,但是什么也没有得到,因为当我尝试将"dilation"步骤的结果转换为CV_8U时,白色对象消失了。当我使用findContours()时,也出现了错误。它说图像通道的一些内容。我不能在这里展示错误,因为那太多步骤了,我已经从我的代码中删除了它。
顺便说一句,在最后,我得到了一个图像的通道。 enter image description here 我可以直接用它来计数吗?还是我必须将其转换,并且最好的方法是什么?

可能是OpenCV如何在二进制图像中找到连接组件列表的重复问题。 - lakshayg
3个回答

3

两个简单步骤:

  1. 找到二值化图像的轮廓。
  2. 获取轮廓数量。

代码:

int count_trees(const cv::Mat& bin_image){
    cv::Mat img;
    if(bin_image.channels()>1){
        cv::cvtColor(bin_image,img,cv::COLOR_BGR2GRAY);
    }
    else{
         img=bin_image.clone();;
    }
    if(img.type()!=CV_8UC1){
        img*=255.f; //This could be stupid, but I do not have an environment to try it
        img.convertTo(img,CV_8UC1);
    }

    std::vector<std::vector<cv::Point>> contours
    std::vector<Vec4i> hierarchy;
    cv::findContours( img, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    return contours.size();
}

谢谢您的回复。我尝试了您的代码,但我仍然收到一个关于不支持格式的错误。错误信息类似于“不支持的格式或格式组合,findContours() 只支持 CV_8u”。 - Reza Kahfi
cvtColor(img,bw,CV_BGR2GRAY); threshold(bw,bw,40,255,CV_THRESH_BINARY); 这些行确保您的图像是CV_8UC1,因为它是作为普通颜色加载的。还有另一段代码导致了这个问题。您应该向我们展示以便我们知道。 - Humam Helfawi
我已经在问题中展示了所有的代码。我真的很想知道为什么会发生这种情况。我从你的代码中创建了一个新的函数,输入参数是膨胀。 - Reza Kahfi
哦,抱歉!我刚刚注意到你是在距离变换之后使用它,这会改变深度。我会编辑我的答案。 - Humam Helfawi
和以前一样。这是错误日志 https://www.dropbox.com/s/nt1q8e1m7f1t5fu/error.JPG?dl=0 - Reza Kahfi
1
轮廓查找是不必要的。这个问题可以通过使用连通组件分析高效地解决。OpenCV有一个“标记”算法可以做到这一点。 - Cris Luengo

0

我有同样的问题,这里是我即将实现的一个想法。

1)将您的图像表示为整数数组;0 = 黑色,1 = 白色

2)设置N = 2

3)逐像素扫描您的图像。每当您找到一个白色像素时,请激活洪水填充算法,从刚刚找到的像素开始;用N ++的值绘制该区域;

4)重复步骤3直到到达最后一个像素。 (N-2)是发现的区域数量。

此方法取决于对象的形状;我的比你的更混乱(祝我好运..)。我将使用在某个地方找到的递归洪水填充配方(也许是Rosetta Code)。

此解决方案还使计算每个区域的大小变得容易。


这是连通组件分析。这是正确的方法。但是还有更有效率的算法。在OpenCV文档中搜索“label”。 - Cris Luengo

-1
尝试将其应用于您删除的图像。
// count
for (int i = 0; i< contours.size(); i = hierarchy[i][0]) // iteration sur chaque contour .
{
    Rect r = boundingRect(contours[i]);
    if (hierarchy[i][2]<0) {
        rectangle(canny_output, Point(r.x, r.y), Point(r.x + r.width, r.y + r.height), Scalar(20, 50, 255), 3, 8, 0);
        count++;
    }
}
cout << "Numeber of contour = " << count << endl;
imshow("src", src);
imshow("contour", dst);
waitKey(0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接