无法检测到任何白色斑点-opencv python [英] unable to detect any white blobs - opencv python
问题描述
我正试图在图像中简单地找到白色的圆形斑点.当我尝试使用houghcircles时,我总是把字母和数字误认为圆圈,而我只需要完整的圆圈,即斑点.
I am trying to simply find white circular blobs in an image. When i tried going with houghcircles, I keep getting letters and digits mistaken for circles, whereas I only need complete circles, i.e. blobs.
因此,我使用自定义参数运行了该斑点检测器代码,以在左上角找到圆形的IC引脚标记.但是我一直在"9"和"0"内出现黑眼圈. 它根本检测不到任何白色斑点.
So I ran this blob detector code with custom params to find the circular IC pin mark on the upper left corner. But I keep getting dark circles inside '9' and '0'. It doesnt detect any white blobs at all.
这是我尝试的代码:
import cv2
import numpy as np;
# Read image
img = cv2.imread("C:\chi4.jpg", cv2.IMREAD_GRAYSCALE)
retval, threshold = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10;
params.maxThreshold = 255;
blur = cv2.GaussianBlur(img,(5,5),0)
params.filterByCircularity = True
params.minCircularity = 0.2
params.filterByArea = True;
params.minArea = 1000;
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Set up the detector with default parameters.
#detector = cv2.SimpleBlobDetector()
# Detect blobs.
keypoints = detector.detect(threshold)
# 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(img, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)
这是我的输出图像:
Here is my output image:
红色圆圈是检测到的圆圈.我希望检测到顶部的白色圆形斑点,并用蓝色标记.
The red circles are the detected ones. I want the white circular blob, in the top marked in blue to be detected.
我尝试更改阈值参数,但仍然没有影响.请让我知道我要去哪里错了,或者提出改善输出的建议. 在此先感谢:D
I tried changing the threshold parameters, still no impact. Kindly let me know where I am going wrong or suggestions to improve the output. Thanks in advance :D
推荐答案
斑点通常假定为灰色/黑色.在您的情况下,字母旁边的斑点为黑色.但是,您想要的斑点是白色的.因此无法识别.
Blobs are generally assumed to be gray/black. In your case the blobs in side the letters are black. However the blob you have intended is white. Hence it Is not recognized.
在您的代码中,您必须将第四行更改为:
In your code you have to change the fourth line to:
retval, threshold = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY_INV)
我在以下方面采用了相同的方法:
I followed the same approach for the following:
我对以下图像进行了斑点检测:
I performed blob detection on the following image:
但未找到任何斑点:
我将二进制图像反转为以下内容:
I inverted the binary image to the following:
现在我能够检测到它们,如下所示:
now I was able to detect them as shown:
这篇关于无法检测到任何白色斑点-opencv python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!