Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/CenterofBlob/single_blob.cpp
3118 views
1
#include "opencv2/highgui/highgui.hpp"
2
#include "opencv2/imgproc/imgproc.hpp"
3
#include <iostream>
4
#include <stdio.h>
5
#include <stdlib.h>
6
7
using namespace cv;
8
using namespace std;
9
10
int main(int argc, char** argv)
11
{
12
// Declare Mat type images
13
Mat src, gray,thr;
14
15
//Load source image, convert it to gray
16
src = imread(argv[1], 1 );
17
18
// convert image to grayscale
19
cvtColor( src, gray, COLOR_BGR2GRAY );
20
21
// convert grayscale to binary image
22
threshold( gray, thr, 100,255,THRESH_BINARY );
23
24
// find moments of the image
25
Moments m = moments(thr,true);
26
Point p(m.m10/m.m00, m.m01/m.m00);
27
28
// coordinates of centroid
29
cout<< Mat(p)<< endl;
30
31
// show the image with a point mark at the centroid
32
circle(src, p, 5, Scalar(128,0,0), -1);
33
imshow("Image with center",src);
34
waitKey(0);
35
}
36
37