-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathtutorial56_simple_blob_detector.py
88 lines (65 loc) · 2.63 KB
/
tutorial56_simple_blob_detector.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#Video Playlist: https://www.youtube.com/playlist?list=PLHae9ggVvqPgyRQQOtENr6hK0m1UquGaG
"""
https://www.learnopencv.com/blob-detection-using-opencv-python-c/
BLOB stands for Binary Large OBject and refers to a group of connected pixels in a binary image.
A Blob is a group of connected pixels in an image that share some common
property ( E.g grayscale value ). In the image above, the dark connected regions are blobs,
and the goal of blob detection is to identify and mark these regions.
How it works:
1. Threshold input images to binary.
2. Grouping: connected white/black pixels are grouped together.
3. Merging: blobs located closer than minDistBetweenBlobs are merged.
4. Center & Radius Calculation : The centers and radii of the new merged blobs are computed and returned.
Can be filtered by color, size or shape
"""
import matplotlib.pyplot as plt
import numpy as np
import cv2
# The input image.
image = cv2.imread("images/cast_iron1.tif", 0)
#image = cv2.imread("images/Osteosarcoma_01_small.tif")
#Extract only blue channel as DAPI / nuclear (blue) staining is the best
#channel to perform cell count.
#image=image[:,:,0]
#No need to pre-threshold as blob detector has build in threshold.
#We can supply a pre-thresholded image.
# Set up the SimpleBlobdetector with default parameters.
params = cv2.SimpleBlobDetector_Params()
# Define thresholds
#Can define thresholdStep. See documentation.
params.minThreshold = 0
params.maxThreshold = 255
# Filter by Area.
params.filterByArea = True
params.minArea = 50
params.maxArea = 10000
# Filter by Color (black=0)
params.filterByColor = False #Set true for cast_iron as we'll be detecting black regions
params.blobColor = 0
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.5
params.maxCircularity = 1
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.5
params.maxConvexity = 1
# Filter by InertiaRatio
params.filterByInertia = True
params.minInertiaRatio = 0
params.maxInertiaRatio = 1
# Distance Between Blobs
params.minDistBetweenBlobs = 0
# Setup the detector with parameters
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs
keypoints = detector.detect(image)
print("Number of blobs detected are : ", len(keypoints))
# Draw blobs
img_with_blobs = cv2.drawKeypoints(image, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
plt.imshow(img_with_blobs)
cv2.imshow("Keypoints", img_with_blobs)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save result
cv2.imwrite("particle_blobs.jpg", img_with_blobs)