diff --git a/CenterofBlob/README.md b/CenterofBlob/README.md new file mode 100644 index 0000000..be83e9f --- /dev/null +++ b/CenterofBlob/README.md @@ -0,0 +1,27 @@ +## Center of Blob using Python and C++ + +To run the code to find center of a single blob, run the following commands:- + +For python:- + +`python3 single_blob.py --ipimage image_name` + + +For C++:- + +1. ``g++ single_blob.cpp `pkg-config opencv --cflags --libs` -o output`` + +2. `./output image_name` + +To run the code to find center of multiple blobs, run the following commands:- + +For python:- + +`python3 center_of_multiple_blob.py --ipimage image_name` + +For C++:- + +1. ``g++ center_of_multiple_blob.cpp `pkg-config opencv --cflags --libs` -o output`` + +2. `./output image_name` + diff --git a/CenterofBlob/circle.png b/CenterofBlob/circle.png new file mode 100644 index 0000000..c4ac205 Binary files /dev/null and b/CenterofBlob/circle.png differ diff --git a/CenterofBlob/multiple-blob.png b/CenterofBlob/multiple-blob.png new file mode 100644 index 0000000..8fc2ddd Binary files /dev/null and b/CenterofBlob/multiple-blob.png differ diff --git a/CenterofBlob/single_blob.cpp b/CenterofBlob/single_blob.cpp index 9e161f1..f65c020 100644 --- a/CenterofBlob/single_blob.cpp +++ b/CenterofBlob/single_blob.cpp @@ -1,20 +1,35 @@ -// declare Mat variables, thr, gray and src -Mat thr, gray, src; - -// convert image to grayscale -cvtColor( src, gray, COLOR_BGR2GRAY ); - -// convert grayscale to binary image -threshold( gray, thr, 100,255,THRESH_BINARY ); - -// find moments of the image -Moments m = moments(thr,true); -Point p(m.m10/m.m00, m.m01/m.m00); - -// coordinates of centroid -cout<< Mat(p)<< endl; - -// show the image with a point mark at the centroid -circle(src, p, 5, Scalar(128,0,0), -1); -imshow("Image with center",src); -waitKey(0); \ No newline at end of file +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/imgproc/imgproc.hpp" +#include +#include +#include + +using namespace cv; +using namespace std; + +int main(int argc, char** argv) +{ + // Declare Mat type images + Mat src, gray,thr; + + //Load source image, convert it to gray + src = imread(argv[1], 1 ); + + // convert image to grayscale + cvtColor( src, gray, COLOR_BGR2GRAY ); + + // convert grayscale to binary image + threshold( gray, thr, 100,255,THRESH_BINARY ); + + // find moments of the image + Moments m = moments(thr,true); + Point p(m.m10/m.m00, m.m01/m.m00); + + // coordinates of centroid + cout<< Mat(p)<< endl; + + // show the image with a point mark at the centroid + circle(src, p, 5, Scalar(128,0,0), -1); + imshow("Image with center",src); + waitKey(0); +} diff --git a/CenterofBlob/single_blob.py b/CenterofBlob/single_blob.py index 81a4738..0086638 100644 --- a/CenterofBlob/single_blob.py +++ b/CenterofBlob/single_blob.py @@ -1,3 +1,16 @@ +import cv2 +import numpy as np +import argparse + +# create object to pass argument +arg_parse = argparse.ArgumentParser() +arg_parse.add_argument("-i", "--ipimage", required=True, + help="input image path") +args = vars(arg_parse.parse_args()) + +# read image through command line +img = cv2.imread(args["ipimage"]) + # convert image to grayscale image gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) @@ -17,4 +30,4 @@ cv2.putText(img, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, ( # display the image cv2.imshow("Image", img) -cv2.waitKey(0) \ No newline at end of file +cv2.waitKey(0)