Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/BlobDetector/blob.py
3118 views
1
#!/usr/bin/python
2
3
# Standard imports
4
import cv2
5
import numpy as np
6
7
# Read image
8
im = cv2.imread("blob.jpg", cv2.IMREAD_GRAYSCALE)
9
10
# Setup SimpleBlobDetector parameters.
11
params = cv2.SimpleBlobDetector_Params()
12
13
# Change thresholds
14
params.minThreshold = 10
15
params.maxThreshold = 200
16
17
18
# Filter by Area.
19
params.filterByArea = True
20
params.minArea = 1500
21
22
# Filter by Circularity
23
params.filterByCircularity = True
24
params.minCircularity = 0.1
25
26
# Filter by Convexity
27
params.filterByConvexity = True
28
params.minConvexity = 0.87
29
30
# Filter by Inertia
31
params.filterByInertia = True
32
params.minInertiaRatio = 0.01
33
34
# Create a detector with the parameters
35
ver = (cv2.__version__).split('.')
36
if int(ver[0]) < 3 :
37
detector = cv2.SimpleBlobDetector(params)
38
else :
39
detector = cv2.SimpleBlobDetector_create(params)
40
41
42
# Detect blobs.
43
keypoints = detector.detect(im)
44
45
# Draw detected blobs as red circles.
46
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
47
# the size of the circle corresponds to the size of blob
48
49
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
50
51
# Show blobs
52
cv2.imshow("Keypoints", im_with_keypoints)
53
cv2.waitKey(0)
54
55
56