Search
 
SCRIPT & CODE EXAMPLE
 

CPP

find number of 1s in a binary cv::mat 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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: convert c program to c++ online 
Cpp :: c++ string to vector using delimiter 
Cpp :: To toggle (flip the status of) the k-th item of the set 
Cpp :: how to print std::string 
Cpp :: cosnt cast 
Cpp :: multiple inheritance c++ 
Cpp :: C++ Function-style Casting 
Cpp :: result += a +b in c++ meaning 
Cpp :: sro in c++ 
Cpp :: 1047. Remove All Adjacent Duplicates In String solution leetcode in c++ 
Cpp :: 378. Kth Smallest Element in a Sorted Matrix using binary search 
Cpp :: fibonacci search algorithm c++ 
Cpp :: spyder enviroment 
Cpp :: 7 9 C:UsersAliyahDocumentsshut up.cpp [Error] expected unqualified-id before string constant 
Cpp :: how to complie c++ to spesific name using terminal 
Cpp :: factorial MOD 998244353 
Cpp :: subsets of a given array 
Cpp :: Chef and IPC Certificates codechef solution in c++ 
Cpp :: multilevel inheritance in c++ private method 
Cpp :: Change Font ImGui 
Cpp :: bounded and unbounded solution in lpp 
Cpp :: assoc-right antlr 
Cpp :: how to analyse a poem 
Cpp :: auto i cpp 
Cpp :: stack in c++ 
Cpp :: are arrays faster than vectors c++ 
Cpp :: ceil value in c++ using formula 
Cpp :: qt graphics scene map cursor position 
C :: how to get time and date in c 
C :: space after format specifiers in c 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =