-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbug_detection.py
60 lines (46 loc) · 1.46 KB
/
bug_detection.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
import cv2
import numpy as np
import utils
# start with colored image
pathImage = "stickytraps/1170.jpg"
widthImg = 600
heightImg = 800
#Gray scale and blur the image
img = cv2.imread(pathImage)
imgGray = cv2.imread(pathImage, cv2.IMREAD_GRAYSCALE)
imgBlur = cv2.GaussianBlur(imgGray, (7, 7), 1)
height, width, channels = img.shape
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params.filterByArea = True
params.minArea = 400
#params.maxArea = 3000
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.1
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.1
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)
#detector = cv2.SimpleBlobDetector_create()
keypoints = detector.detect(imgBlur)
print(len(keypoints))
imgKeyPoints = cv2.drawKeypoints(img, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Crop out keypoints
str = ""
for keypoint in keypoints:
str += "1"
x = int(keypoint.pt[0])
y = int(keypoint.pt[1])
size = int(keypoint.size)
cv2.imshow(str, img[max(1,y-2*size): min(height-1,y+2*size), max(1,x-2*size): min(width-1,x+2*size)])
# Display found keypoints
imgKeyPoints = cv2.resize(imgKeyPoints, (2000, 1400))
cv2.imshow("Keypoints", imgKeyPoints)
#cv2.imshow("grays", imgGray)
#cv2.imshow("blur", imgBlur)
#cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()