[英]Blob detection using OpenCV
我正在尝试使用OpenCV进行一些白色斑点检测。 但是我的脚本无法检测到大白块,这是我的目标,同时检测到一些小斑点。 我是OpenCV的新手,在OpenCV中使用simpleblobdetection时我做错什么了吗? [部分解决,请阅读以下内容]
这是脚本:
#!/usr/bin/python
# Standard imports
import cv2
import numpy as np;
from matplotlib import pyplot as plt
# Read image
im = cv2.imread('whiteborder.jpg', cv2.IMREAD_GRAYSCALE)
imfiltered = cv2.inRange(im,255,255)
#OPENING
kernel = np.ones((5,5))
opening = cv2.morphologyEx(imfiltered,cv2.MORPH_OPEN,kernel)
#write out the filtered image
cv2.imwrite('colorfiltered.jpg',opening)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()
params.blobColor= 255
params.filterByColor = True
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(opening)
# Draw detected blobs as green circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
print str(keypoints)
im_with_keypoints = cv2.drawKeypoints(opening, keypoints, np.array([]), (0,255,0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show blobs
##cv2.imshow("Keypoints", im_with_keypoints)
cv2.imwrite('Keypoints.jpg',im_with_keypoints)
cv2.waitKey(0)
编辑 :
通过添加更大的面积最大值,我可以识别出一个大斑点,但我的最终目标是识别出是否存在一个大的白色矩形。 我所做的白色斑点检测不仅返回矩形,而且还返回周围的区域。 [本部分解决]
编辑2:
根据@PSchn的回答,我更新了代码以应用逻辑,首先将滤色器设置为仅获取白色像素,然后使用开口消除噪点。 它适用于示例数据,在检测到斑点后我可以成功获取关键点。
您可以尝试将params.maxArea设置为令人讨厌的大小(数万个左右):默认值可能小于您要检测的矩形的面积。 另外,我不知道这是不是真的,但我听说颜色检测存在逻辑错误,因此,有必要尝试禁用它,以防万一导致问题出现(这可能已经已在更高版本中修复,但仍然值得一试)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.