【问题标题】:Blob detection in Python?Python中的斑点检测?
【发布时间】:2020-08-09 10:35:16
【问题描述】:

我正在尝试从下图中检测到一个 blob。我使用了 skimage 并使用了手册中解释的 3 种不同方法,但它无法检测到灰色斑点。这是原图:

所以我尝试了以下代码:

from math import sqrt
import cv2
from skimage.feature import blob_dog, blob_log, blob_doh
from skimage.color import rgb2gray
import matplotlib.pyplot as plt

image = cv2.imread("blob800_cropped.png")
image_gray = rgb2gray(image)

blobs_log = blob_log(image_gray, max_sigma=30, num_sigma=10, threshold=.05)

# Compute radii in the 3rd column.
blobs_log[:, 2] = blobs_log[:, 2] * sqrt(2)

blobs_dog = blob_dog(image_gray, max_sigma=30, threshold=.05)
blobs_dog[:, 2] = blobs_dog[:, 2] * sqrt(2)

blobs_doh = blob_doh(image_gray, max_sigma=30, threshold=.01)

blobs_list = [blobs_log, blobs_dog, blobs_doh]
colors = ['yellow', 'lime', 'red']
titles = ['Laplacian of Gaussian', 'Difference of Gaussian',
          'Determinant of Hessian']
sequence = zip(blobs_list, colors, titles)

fig, axes = plt.subplots(1, 3, figsize=(9, 3), sharex=True, sharey=True)
ax = axes.ravel()

for idx, (blobs, color, title) in enumerate(sequence):
    ax[idx].set_title(title)
    ax[idx].imshow(image)
    for blob in blobs:
        y, x, r = blob
        c = plt.Circle((x, y), r, color=color, linewidth=2, fill=False)
        ax[idx].add_patch(c)
    ax[idx].set_axis_off()

plt.tight_layout()
plt.show()

但是,没有检测到我正在寻找的 blob:

这是我期待的输出:

【问题讨论】:

    标签: python opencv scikit-image cv2


    【解决方案1】:

    这是我在 Python/OpenCV 中的做法。

    • 读取输入
    • 转换为灰色
    • 应用极端自适应阈值
    • 应用形态学打开和关闭以移除小区域
    • 获取轮廓并保存最大的
    • 在输入上绘制最大轮廓
    • 保存结果

    输入:

    import cv2
    import numpy as np
    
    # read image
    img = cv2.imread("doco3.jpg")
    
    # convert img to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # do adaptive threshold on gray image
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 101, 3)
    
    # apply morphology open then close
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
    blob = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
    blob = cv2.morphologyEx(blob, cv2.MORPH_CLOSE, kernel)
    
    # invert blob
    blob = (255 - blob)
    
    # Get contours
    cnts = cv2.findContours(blob, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    big_contour = max(cnts, key=cv2.contourArea)
    
    # test blob size
    blob_area_thresh = 1000
    blob_area = cv2.contourArea(big_contour)
    if blob_area < blob_area_thresh:
        print("Blob Is Too Small")
    
    # draw contour
    result = img.copy()
    cv2.drawContours(result, [big_contour], -1, (0,0,255), 1)
    
    # write results to disk
    cv2.imwrite("doco3_threshold.jpg", thresh)
    cv2.imwrite("doco3_blob.jpg", blob)
    cv2.imwrite("doco3_contour.jpg", result)
    
    # display it
    cv2.imshow("IMAGE", img)
    cv2.imshow("THRESHOLD", thresh)
    cv2.imshow("BLOB", blob)
    cv2.imshow("RESULT", result)
    cv2.waitKey(0)
    


    阈值图像:

    Blob 的形态清洁图像:

    输入的结果轮廓:

    【讨论】:

    • 这太好了,谢谢。一个快速的问题,我应该如何为 blob 设置限制大小?例如,如果最大 blob 大小小于某个值,我希望能够返回“No Blob”。感谢您的帮助@fmw42
    • big_contour=... blob_area = cv2.contourArea(big_contour)if blob_area &lt; blob_area_thresh: print("Too Small") 之后添加,其中 blob_area_thresh 是您的区域限制。在我的答案中查看我的变化
    • 有史以来最好的回应!谢谢
    猜你喜欢
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    • 2020-09-17
    • 1970-01-01
    • 2011-09-04
    • 2012-04-05
    相关资源
    最近更新 更多