OpenCV blob 检测器未检测到白色斑点

OpenCV blob detector isn't detecting white blobs

提问人:coMPUTER sCIENCE sTUDENT 提问时间:7/12/2019 最后编辑:nathancycoMPUTER sCIENCE sTUDENT 更新时间:7/12/2019 访问量:1791

问:

我正在尝试计算阈值后的白点数量,但我的代码似乎没有检测到任何东西

输入图像

enter image description here

#Standard imports
#!/usr/bin/python

# Standard imports
import cv2
import numpy as np;

# Read and threshold image
im = cv2.imread("CopperSEM.tif", cv2.IMREAD_GRAYSCALE)
ret2, LocalTH1 = cv2.threshold(im,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) #Without Filtering

# Set up the detector with default parameters.
parameters = cv2.SimpleBlobDetector_Params()

#change Colors to White
parameters.filterByColor = True
parameters.blobColor = 255
parameters.filterByArea = True
parameters.minArea = 1500
parameters.filterByCircularity = True
parameters.minCircularity = 0.1
parameters.filterByConvexity = True
parameters.minConvexity = 0.87


#reset the detector
detector = cv2.SimpleBlobDetector_create(parameters)

# Detect blobs.
keypoints = detector.detect(LocalTH1)
print(len(keypoints)) #will print out the number of objects that were found since keypoints is a list?
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(LocalTH1, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)

我的输出如下

enter image description here

Python OpenCV 图像处理 计算机视觉 轮廓

评论

0赞 andrewchan2022 11/8/2020
parameters.minArea = 1500 太大

答:

4赞 nathancy 7/12/2019 #1

image here

与其使用 blob 检测器,不如使用一种可能更好的方法

  • 转换为灰度和中值模糊以平滑图像
  • 阈值图像
  • 查找轮廓
  • 遍历等值线并使用等值线区域进行过滤

您可以使用最小阈值区域进行筛选,以计算白点的数量。通过减小阈值区域,可以包含较小的点。增加面积仅隔离较大的白色斑点。如果等值线通过此过滤器,则可以将其添加到白点列表中

白点数量

91

import cv2

image = cv2.imread('1.png')

blur = cv2.medianBlur(image, 5)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray,180,255, cv2.THRESH_BINARY)[1]

cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

min_area = 50
white_dots = []
for c in cnts:
    area = cv2.contourArea(c)
    if area > min_area:
        cv2.drawContours(image, [c], -1, (36, 255, 12), 2)
        white_dots.append(c)

print(len(white_dots))
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.imwrite('image.png', image)
cv2.waitKey()

评论

0赞 coMPUTER sCIENCE sTUDENT 7/12/2019
如果 len(cnts) == 2 else cnts[1],cnts = cnts[0] 的函数是什么?
0赞 coMPUTER sCIENCE sTUDENT 7/12/2019
谢谢。你救了我。