参考: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)
GitHub 加速计划 / opencv31 / opencv
142
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9 Animated WebP Support #25608 related issues #24855 #22569 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
09030615 V4l default image size #25500 Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution. Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499) Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551) This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [X] I agree to contribute to the project under Apache 2 License. - [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [X] The PR is proposed to the proper branch - [X] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐