【问题标题】:How to get pixel locations of all the white blobs如何获取所有白色斑点的像素位置
【发布时间】:2019-12-10 18:32:15
【问题描述】:

我正在从二进制图像创建注释。我想要二进制图像中每个白色 blob 列表的单独列表。所以按照我在下面给出的图像,我应该得到六个数组/列表。

我在Numpy + OpenCV-Python : Get coordinates of white pixels 之后使用了 np.argwhere(img == 255),但它返回了所有白色斑点的组合像素位置。

我已经使用了这个 cv2.findContours(threshed, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2] 但这会返回外边缘的像素值而不是整个 blob。 如何从图像中获取单独的列表?

【问题讨论】:

  • 他们总是小圈子吗?
  • 您可以使用斑点检测或连接组件来获取质心。或者您可以使用轮廓,然后获取轮廓的质心或获取轮廓周围的边界框并计算边界框的中心。

标签: python


【解决方案1】:

你可以使用connectedComponents:

r,l = cv2.connectedComponents(img)
blobs = [np.argwhere(l==i) for i in range(1,r)]

(标签0是背景,这就是为什么我们在range(1,r)中以1开头)

例子:

import cv2
from urllib.request import urlopen
import numpy as np

req = urlopen('https://i.stack.imgur.com/RcQut.jpg')
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr, cv2.IMREAD_GRAYSCALE) 
img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1]

r,l = cv2.connectedComponents(img)

print([len(np.argwhere(l==i)) for i in range(1,r)]) #just the lenghts for shortness
#[317, 317, 377, 797, 709, 613]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2012-04-05
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多