簡體   English   中英

如何計算二進制圖像上的白色物體?

[英]How to count white object on Binary Image?

我正試圖從圖像中計算對象。 我使用日志照片,並使用一些步驟來獲取二進制圖像。 在此輸入圖像描述

這是我的代碼:

#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()時也遇到了錯誤。 它講述了關於圖像通道的一些信息。 我無法在這里顯示錯誤,因為這個步驟太多了,我已經從代碼中刪除了它。

順便說一句,最后,我得到了一個圖像通道。 在此輸入圖像描述 我可以用它來計算,或者我必須轉換它,最好的方法是什么?

兩個簡單的步驟:

  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();
}

我有同樣的問題,這是我即將實施的想法。

1)將圖像表示為整數數組; 0 = black, 1 = white

2)設置N = 2 ;

3)逐個像素地掃描圖像。 每當你找到一個白色像素時,激活一個泛光填充算法,從剛找到的像素開始; N++的值繪制區域;

4)迭代3直到你到達最后一個像素。 N-2 )是找到的區域數量。

這種方法取決於物體的形狀; 我的比你的更混亂(祝我好運..)。 我將使用在某處找到的遞歸洪水填充配方(可能是Rosetta Code)。

此解決方案還可以輕松計算每個區域的大小。

嘗試將其應用於您刪除的img

// 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM