Skip to content

Commit

Permalink
updated code
Browse files Browse the repository at this point in the history
  • Loading branch information
krutikabapat committed Jul 23, 2018
1 parent 7b311b6 commit 8fc07d9
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 21 deletions.
27 changes: 27 additions & 0 deletions CenterofBlob/README.md
Original file line number Diff line number Diff line change
@@ -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`

Binary file added CenterofBlob/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added CenterofBlob/multiple-blob.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 35 additions & 20 deletions CenterofBlob/single_blob.cpp
Original file line number Diff line number Diff line change
@@ -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);
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

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);
}
15 changes: 14 additions & 1 deletion CenterofBlob/single_blob.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -17,4 +30,4 @@

# display the image
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.waitKey(0)

0 comments on commit 8fc07d9

Please sign in to comment.