gpt4 book ai didi

python - Python 中的 Blob 检测?

转载 作者:行者123 更新时间:2023-12-02 16:16:20 27 4
gpt4 key购买 nike

我正在尝试从下图中检测 Blob 。我使用了 skimage 并使用了手册中介绍的 3 种不同方法,但它无法检测到灰色 Blob 。这是原始图像:

enter image description here

所以我尝试了下面的代码:

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:

enter image description here

这是我期待的输出:

enter image description here

最佳答案

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

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

输入:

enter image description here

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)


阈值图像:

enter image description here

blob 的形态学清理图像:

enter image description here

输入的结果轮廓:

enter image description here

关于python - Python 中的 Blob 检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61432335/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com