【问题标题】:Python Conjoined Blob DetectionPython 联合 Blob 检测
【发布时间】:2021-03-20 04:15:29
【问题描述】:

我正在尝试对花生形状的分布进行斑点检测 - 典型示例见下图:

我想找到这两个 blob 的质心,但我不确定哪种计算机视觉方法或算法最适合解决这个问题。理想情况下,解决方案应该是轻量级的,因为我需要处理许多这样的 blob。到目前为止,我已经尝试使用 skimage.feature.blob_doh 来获取 blob 中心和半径,并找到该 blob 的加权平均像素,但由于 blob_doh 与 blob 的连接性质(即只返回一个 blob):

blobbing代码如下:

def get_blobs(image, num_blobs):
    img = image
    img = cv.normalize(img, None, 0.0, 1.0, cv.NORM_MINMAX)
    img = img.astype(float)
    blobs = blob_doh(img)
    ys = np.array([x[0] for x in blobs])
    xs = np.array([x[1] for x in blobs])
    rs = np.array([x[2] for x in blobs])
    sort_args = np.argsort(rs)
    xs, ys, rs = xs[sort_args], ys[sort_args], rs[sort_args]
    if len(ys) < num_glints:
        diff = num_blobs - len(ys)
        ys = np.concatenate((ys, np.zeros((diff)))).astype(float)
        xs = np.concatenate((xs, np.zeros((diff)))).astype(float)
        rs = np.concatenate((rs, np.zeros((diff)))).astype(float)
        num_blobs = len(ys)
    xs, ys, rs = xs[0:num_blobs], ys[0:num_blobs], rs[0:num_blobs]

    return xs, ys, rs

我也不想对图像进行二值化,因为我发现质心是这样的:

def find_centroid(xs, ys, weights):
    sum_w = np.sum(weights)
    sum_x = np.sum(np.multiply(xs, weights))
    sum_y = np.sum(np.multiply(ys, weights))
    cx = sum_x / sum_w
    cy = sum_y / sum_w
    return [cx, cy]

(换句话说,斑点中的所有像素都对质心有贡献)。 非常感谢!

【问题讨论】:

  • 不确定您是否尝试过,但您可能会发现,将一些形态学操作作为预处理步骤(侵蚀或打开)简单地应用于图像可能有助于拆分这些斑点。

标签: python opencv scipy computer-vision


【解决方案1】:

以下是使用 OpenCV 的一种方法。然而,这将使图像二值化,但不应该有任何性能缺陷。您可以根据需要调整阈值参数,还可以根据需要添加对 blob 大小的过滤。

import numpy as np
import cv2
import matplotlib.pyplot as plt

rgb = cv2.imread('/your/image/path/blobs_0001.png')
gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)

th = cv2.adaptiveThreshold(gray,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,11,2)

contours, _ = cv2.findContours(th,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    M = cv2.moments(cnt)
    cx = int(M['m10']/M['m00'])
    cy = int(M['m01']/M['m00'])
    cv2.circle(rgb, (cx, cy), 5, (255, 0, 0), cv2.FILLED)
    
plt.imshow(rgb)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-21
    • 2016-08-13
    • 2011-10-16
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    相关资源
    最近更新 更多