-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircles.py
41 lines (31 loc) · 1.38 KB
/
circles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# import the necessary packages
import numpy as np
import argparse
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
#ap.add_argument("-i", "--image", required = True, help = "Path to the image")
#args = vars(ap.parse_args())
# load the image, clone it for output, and then convert it to grayscale
image = cv2.imread("C:\Users\Richa\Downloads\iRED.jpg")
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
counter =0
# detect circles in the image
circles = cv2.HoughCircles(gray, cv2.cv.CV_HOUGH_GRADIENT, 10, 40, param1=80, param2= 40,minRadius = 20, maxRadius= 40)
# ensure at least some circles were found
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(output, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(output, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
cropped = image[max(y-r,0):min(y+r,image.shape[0]), max(x-r,0):min(x+r, image.shape[1])]
cv2.imwrite("cell-red"+ str(counter) + ".jpg", cropped)
counter +=1
# show the output image
cv2.imwrite("output.jpg", np.hstack([image, output]))
cv2.waitKey(0)