python - 通过 OpenCV 在原始图像上标记 Blob 检测

标签 python opencv image-processing opencv-python opencv-contour

我想用找到的 blob 标记原始图像。但是每当我进行 blob 检测时,它只会生成一个像这样的新图像:
blob 后的结果图像:
enter image description here
但是,我想显示带有红色标记的原始图像。原图:
enter image description here
我只是使用常规代码进行 blob 检测。有没有另一种方法可以在原始图像上做红圈?所以很清楚他们在哪里。

im_gray = cv2.imread(img,cv2.IMREAD_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 128, 255,cv2.THRESH_BINARY | 
cv2.THRESH_OTSU)

thresh = 50
im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]

#detect blobs based on features
params = cv2.SimpleBlobDetector_Params()

# Filter by Area.
params.filterByArea = True
params.minArea = 70
params.maxArea = 150

# 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.3
params.maxInertiaRatio = 0.9

# Distance Between Blobs
params.minDistBetweenBlobs = 0



#thresholded to value 70 detecting blobs:


detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(im_bw)
print("Number of blobs detected are : ", len(keypoints))
#detect blobs: missing the detection based on features
im_with_keypoints = cv2.drawKeypoints(im_bw, keypoints, numpy.array([]), (0, 0, 255),
                                      cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

最佳答案

你的问题是你的最后一行:

im_with_keypoints = cv2.drawKeypoints(im_bw, keypoints, numpy.array([]), (0, 0, 255),
                                      cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
在这里,您可以在 im_bw 上绘制您的关键点,这不是您的原始图像,而是您的阈值图像。如果更换 im_bw在这里使用您的原始图像(例如,使用您已经加载为 im_gray 的灰度版本),您应该得到您想要的结果。

关于python - 通过 OpenCV 在原始图像上标记 Blob 检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67712915/

相关文章:

image-processing - 如何将通过http post接收到的png图像加载为OpenCV IplImage格式

python - 为什么我需要在 tkinter 函数内部使用全局语句?

python - Python 中实例方法内部 self 的用法

c++ - OpenCV 不会加载图像

python - 用opencv python删除背景

python - 导入cv2.py错误;无法安装 Python cv2

c++ - 如何访问 QPainterPath 下的所有像素

python - 为什么 numpy max 函数(np.max)返回错误的输出?

python - 当我在 Django 中运行完整的测试套件时,我收到有关缺少 MessageMiddleware 的错误

c++ - 如何在opencv中检查白色四边形