-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblob.py
66 lines (57 loc) · 2.08 KB
/
blob.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import cv2
import sys
import numpy as np
def imsplit(name, debug=False):
im = cv2.imread('1'+name)
# Setup BlobDetector
detector = cv2.SimpleBlobDetector_create()
params = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params.filterByArea = True
params.minArea = 2
params.maxArea = 4000000
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1
# Filter by Convexity
params.filterByConvexity = False
#params.minConvexity = 0.87
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.8
# Distance Between Blobs
params.minDistBetweenBlobs = 24
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)
#retval, im = camera.read()
overlay = im.copy()
keypoints = detector.detect(im)
x_start, y_start = 0, 0
x_end, y_end = 0, 0
for k in keypoints:
if debug:
cv2.circle(overlay, (int(k.pt[0]), int(k.pt[1])), int(k.size/2), (0, 0, 255), -1)
#print(int(k.pt[0]))
#print(int(k.pt[1]))
if int(k.pt[0]) > x_start:
x_start = int(k.pt[0])
y_start = int(k.pt[1])
#cv2.line(overlay, (int(k.pt[0])-20, int(k.pt[1])), (int(k.pt[0])+20, int(k.pt[1])), (0,0,0), 3)
#cv2.line(overlay, (int(k.pt[0]), int(k.pt[1])-20), (int(k.pt[0]), int(k.pt[1])+20), (0,0,0), 3)
w,h = 30 ,26
x_start, y_start = x_start - w/2, y_start - h/2
x_end, y_end = x_start + w, y_start + h
opacity = 0.5
cv2.addWeighted(overlay, opacity, im, 1 - opacity, 0, im)
# Uncomment to resize to fit output window if needed
#im = cv2.resize(im, None,fx=0.5, fy=0.5, interpolation = cv2.INTER_CUBIC)
refPoint = [(x_start, y_start), (x_end, y_end)]
roi = im[int(refPoint[0][1]):int(refPoint[1][1]), int(refPoint[0][0]):int(refPoint[1][0])]
cv2.imwrite('2'+name,roi)
if debug:
cv2.imshow("Output", overlay)
cv2.imwrite('2.png',overlay)
cv2.waitKey()
cv2.destroyAllWindows()
print('imcrop done')
return;