参考:Blob Detection Using OpenCV ( Python, C++ )
Bolob检测
Blob 是图像中一组连接的像素,它们共享一些共同属性(例如,灰度值)。在上图中,深色连接区域是 Blob,Blob 检测旨在识别和标记这些区域。
Blob detection 是一种在图像中检测和识别连通区域(blob)的方法。用于检测图像中的斑点或目标区域的方法,它可以识别具有特定属性(如颜色、大小、形状等)的连通区域
可以使用 SimpleBlobDetector 类来实现
参数:
颜色:
首先,你需要将filterByColor设置为1。将blobColor设为0以选择较暗的Blob,设为255则选择较亮的Blob。基于大小:你可以通过设定参数filterByArea为1,并设置合适的minArea和maxArea值,从而根据Blob的大小进行过滤。例如,将minArea设为100,则会过滤掉所有像素面积小于100的Blob。
Circularity 形状:
圆度: 这个参数衡量的是Blob接近圆形的程度。例如,一个规则六边形的圆度比正方形更高。
Convexity 凸性:
Inertia Ratio :衡量一个形状的拉伸程度的。例如,对于一个圆形,这个值是1;对于一个椭圆,其值介于0和1之间;而对于一条线段,其值为0。
设置参数:
# Read image
im = cv2.imread(r'E:\BlobTest.jpg', cv2.IMREAD_GRAYSCALE)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10
params.maxThreshold = 255
# Filter by Area.
params.filterByArea = True
params.minArea = 1500
# Filter by Circularity 与⚪相近 比如八边形
params.filterByCircularity = True
params.minCircularity = 0.1
# Filter by Convexity 有缺口的圆
params.filterByConvexity = True
params.minConvexity = 0.87
# Filter by Inertia 检测到椭圆
# params.filterByInertia = True
# params.minInertiaRatio = 0.01
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
detector = cv2.SimpleBlobDetector(params)
else:
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(im)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# 输出关键点位置
if len(keypoints) > 0:
for kp in keypoints:
print(kp.pt)
al_filter = cv2.bilateralFilter(src=image, d=9, sigmaColor=75, sigmaSpace=75)
# Read image
src = cv2.imread("E://threshold.png", cv2.IMREAD_GRAYSCALE)
# Basic threhold example
th, dst = cv2.threshold(src, 0, 255, cv2.THRESH_BINARY)
cv2.imwrite("opencv-threshold-example.jpg", dst)
# Thresholding with maxValue set to 128
th, dst = cv2.threshold(src, 0, 128, cv2.THRESH_BINARY)
cv2.imwrite("opencv-thresh-binary-maxval.jpg", dst)
# Thresholding with threshold value set 127
th, dst = cv2.threshold(src, 127, 255, cv2.THRESH_BINARY)
cv2.imwrite("opencv-thresh-binary.jpg", dst)
# Thresholding using THRESH_BINARY_INV
th, dst = cv2.threshold(src, 127, 255, cv2.THRESH_BINARY_INV)
cv2.imwrite("opencv-thresh-binary-inv.jpg", dst)
# Thresholding using THRESH_TRUNC
th, dst = cv2.threshold(src, 127, 255, cv2.THRESH_TRUNC)
cv2.imwrite("opencv-thresh-trunc.jpg", dst)
# Thresholding using THRESH_TOZERO
th, dst = cv2.threshold(src, 127, 255, cv2.THRESH_TOZERO)
cv2.imwrite("opencv-thresh-tozero.jpg", dst)
# Thresholding using THRESH_TOZERO_INV
th, dst = cv2.threshold(src, 127, 255, cv2.THRESH_TOZERO_INV)
cv2.imwrite("opencv-thresh-to-zero-inv.jpg", dst)
# Set up the detector with default parameters.
detector = cv2.SimpleBlobDetector()
# Read image
im = cv2.imread(r'E:\BlobTest.jpg', cv2.IMREAD_GRAYSCALE)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10
params.maxThreshold = 255
# Filter by Area.
params.filterByArea = True
params.minArea = 1500
# Filter by Circularity 与⚪相近 比如八边形
params.filterByCircularity = True
params.minCircularity = 0.1
# Filter by Convexity 有缺口的圆
params.filterByConvexity = True
params.minConvexity = 0.87
# Filter by Inertia 检测到椭圆
# params.filterByInertia = True
# params.minInertiaRatio = 0.01
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
detector = cv2.SimpleBlobDetector(params)
else:
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(im)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv_show(im_with_keypoints)
# 输出关键点位置
if len(keypoints) > 0:
for kp in keypoints:
print(kp.pt)