OpenCV-获取所有Blob像素

问题描述 投票:1回答:2

我有这张图片:

enter image description here

我一直在使用功能SimpleBlobDetector来识别黑色背景上白色像素的斑点。代码在下面。

SimpleBlobDetector

可以正确识别斑点,如下图所示:

blobDetectorParameters = cv2.SimpleBlobDetector_Params() blobDetectorParameters.filterByArea = True blobDetectorParameters.minArea = 1 blobDetectorParameters.maxArea = 100 blobDetectorParameters.minDistBetweenBlobs = 1 blobDetectorParameters.filterByCircularity = False blobDetectorParameters.filterByColor = False blobDetectorParameters.filterByConvexity = False blobDetectorParameters.filterByInertia = False detector = cv2.SimpleBlobDetector_create(blobDetectorParameters) keypoints = detector.detect(image) imageWithKeypoints = cv2.drawKeypoints(image, keypoints, numpy.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) cv2.imshow("Keypoints", imageWithKeypoints) cv2.waitKey(0)

问题:我希望能够列出所有斑点像素,以便将它们画出来。我似乎找不到使用enter image description here返回所有斑点的像素的方法。我可以获得关键点(通过SimpleBlobDetector返回),但我相信这些关键点对应于斑点中心。

是否有与detect相关的函数(或OpenCV中的其他地方)以返回与all斑点相关的all像素?

谢谢您的帮助。

python opencv image-processing blob pixel
2个回答
0
投票

我认为对于您的用例,您可能会从SimpleBlobDetector中受益更多。您甚至会看到SimpleBlobDetector使用此功能。您提供的链接的第2步中提到了此方法,但知道轮廓仅列出了外部像素。如果您要做的只是简单地绘制每个斑点,那么在使用drawContours时,只需将厚度参数设置为-1。

如果您需要知道每个像素值,那么我想说的解决方案是绘制每个轮廓,然后将像素值记录到您自己的列表中。尚不清楚您正试图将其用于什么目的,因此很难给您一个直接的答案,但我希望这会有所帮助。


0
投票

您可以在二进制图像上使用findContours + np.column_stack来获取所有点的坐标。在此示例中,我将每个点都涂成绿色,然后涂上新的蒙版。结果是

np.where

这里是每个像素的enter image description here坐标

(x,y)

代码

[[ 28  32]
 [ 28  33]
 [ 29  33]
 [ 31  25]
 [ 31  26]
 [ 37  43]
 [ 37  44]
 [ 37  45]
 [ 38  43]
 [ 38  44]
 [ 38  45]
 [ 85  96]
 [118 116]
 [118 118]
 [119 116]
 [119 117]
 [120 116]
 [121  87]
 [121 115]
 [122  87]
 [122 115]
 [123  87]
 [123  97]
 [123 115]
 [124  87]
 [124  97]
 [124 115]
 [125  93]
 [125  95]
 [125  96]
 [125 114]
 [125 115]
 [126  94]
 [126  95]
 [126  96]
 [126 113]
 [126 114]
 [127  90]
 [127  94]
 [127  95]
 [127  96]
 [127 112]
 [127 113]
 [128  90]
 [128  91]
 [128  95]
 [128 102]
 [128 103]
 [128 104]
 [128 111]
 [128 112]
 [129 101]
 [129 102]
 [129 103]
 [129 104]
 [130  84]
 [130  85]
 [130 101]
 [130 102]]
© www.soinside.com 2019 - 2024. All rights reserved.