📜  在二进制 cv::mat 图像中查找 1 的数量 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:52.548000             🧑  作者: Mango

代码示例1
#include 
#include 
#include 
#include 
#include 
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