Skip to content

Commit 7b311b6

Browse files
committed
Center of Blob
1 parent befceac commit 7b311b6

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
RNG rng(12345);
11+
12+
void find_moments( Mat src );
13+
14+
int main(int argc, char** argv)
15+
{
16+
/// Load source image, convert it to gray
17+
Mat src, gray;
18+
src = imread(argv[1], 1 );
19+
20+
cvtColor( src, gray, COLOR_BGR2GRAY );
21+
22+
namedWindow( "Source", WINDOW_AUTOSIZE );
23+
imshow( "Source", src );
24+
// call function to find_moments
25+
find_moments( gray );
26+
27+
waitKey(0);
28+
return(0);
29+
}
30+
31+
void find_moments( Mat gray )
32+
{
33+
Mat canny_output;
34+
vector<vector<Point> > contours;
35+
vector<Vec4i> hierarchy;
36+
37+
/// Detect edges using canny
38+
Canny( gray, canny_output, 50, 150, 3 );
39+
// Find contours
40+
findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
41+
42+
/// Get the moments
43+
vector<Moments> mu(contours.size() );
44+
for( int i = 0; i < contours.size(); i++ )
45+
{ mu[i] = moments( contours[i], false ); }
46+
47+
/// Get the centroid of figures.
48+
vector<Point2f> mc( contours.size() );
49+
for( int i = 0; i < contours.size(); i++ )
50+
{ mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
51+
52+
53+
/// Draw contours
54+
55+
Mat drawing(canny_output.size(), CV_8UC3, Scalar(255,255,255));
56+
57+
for( int i = 0; i< contours.size(); i++ )
58+
{
59+
Scalar color = Scalar(167,151,0);
60+
drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
61+
circle( drawing, mc[i], 4, color, -1, 7, 0 );
62+
}
63+
64+
/// Show the resultant image
65+
namedWindow( "Contours", WINDOW_AUTOSIZE );
66+
imshow( "Contours", drawing );
67+
waitKey(0);
68+
69+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import cv2
2+
import numpy as np
3+
import argparse
4+
5+
# create object to pass argument
6+
arg_parse = argparse.ArgumentParser()
7+
arg_parse.add_argument("-i", "--ipimage", required=True,
8+
help="input image path")
9+
args = vars(arg_parse.parse_args())
10+
11+
# read image through command line
12+
img = cv2.imread(args["ipimage"])
13+
# convert the image to grayscale
14+
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
15+
# convert the grayscale image to binary image
16+
ret,thresh = cv2.threshold(gray_image,127,255,0)
17+
18+
# find contour in the binary image
19+
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
20+
for c in contours:
21+
# calculate moments for each contour
22+
M = cv2.moments(c)
23+
cX = int(M["m10"] / M["m00"])
24+
cY = int(M["m01"] / M["m00"])
25+
26+
27+
# calculate x,y coordinate of center
28+
cv2.circle(img, (cX, cY), 5, (255, 255, 255), -1)
29+
cv2.putText(img, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
30+
# display the image
31+
cv2.imshow("Image", img)
32+
cv2.waitKey(0)
33+
34+
# 3.4.1 im2, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
35+
# 3.2.0 im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
36+
37+

CenterofBlob/single_blob.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// declare Mat variables, thr, gray and src
2+
Mat thr, gray, src;
3+
4+
// convert image to grayscale
5+
cvtColor( src, gray, COLOR_BGR2GRAY );
6+
7+
// convert grayscale to binary image
8+
threshold( gray, thr, 100,255,THRESH_BINARY );
9+
10+
// find moments of the image
11+
Moments m = moments(thr,true);
12+
Point p(m.m10/m.m00, m.m01/m.m00);
13+
14+
// coordinates of centroid
15+
cout<< Mat(p)<< endl;
16+
17+
// show the image with a point mark at the centroid
18+
circle(src, p, 5, Scalar(128,0,0), -1);
19+
imshow("Image with center",src);
20+
waitKey(0);

CenterofBlob/single_blob.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# convert image to grayscale image
2+
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
3+
4+
# convert the grayscale image to binary image
5+
ret,thresh = cv2.threshold(gray_image,127,255,0)
6+
7+
# calculate moments of binary image
8+
M = cv2.moments(thresh)
9+
10+
# calculate x,y coordinate of center
11+
cX = int(M["m10"] / M["m00"])
12+
cY = int(M["m01"] / M["m00"])
13+
14+
# put text and highlight the center
15+
cv2.circle(img, (cX, cY), 5, (255, 255, 255), -1)
16+
cv2.putText(img, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
17+
18+
# display the image
19+
cv2.imshow("Image", img)
20+
cv2.waitKey(0)

0 commit comments

Comments
 (0)