为什么blob检测的结果在参数更改后没有改变?

时间:2016-08-16 16:02:41

标签: python opencv blob

我在以下网站上获得了斑点检测程序:substr()

安静有用,但在更改参数值后,我发现结果没有任何变化。 喜欢:即使我将颜色参数设置为255(它用于检测更轻的斑点),仍然可以检测到黑色斑点。此外,在我更改最小区域的值后,也可以检测到最小的斑点。

似乎没有任何变化,结果总是如下: https://www.learnopencv.com/blob-detection-using-opencv-python-c/ 这是代码:

import cv2
import numpy as np;

# Read image
im = cv2.imread("blob.jpg", cv2.IMREAD_GRAYSCALE)


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

params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 10;    # the graylevel of images
params.maxThreshold = 200;

params.filterByColor = True
params.blobColor = 255

# Filter by Area.
params.filterByArea = False
params.minArea = 10000

# Detect blobs.
keypoints = detector.detect(im)

# 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(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

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

任何人都可以帮助我吗?非常感谢!!!

1 个答案:

答案 0 :(得分:0)

您可以更改参数,但探测器不会使用它们。设置它们然后设置检测器:

import cv2
import numpy as np

# Read image
im = cv2.imread('blob.jpg', cv2.IMREAD_GRAYSCALE)

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 10    # the graylevel of images
params.maxThreshold = 200

# Filter by Area.
params.filterByArea = True
params.minArea = 1500

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector(params)

# Detect blobs.
keypoints = detector.detect(im)

# 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(
    im, keypoints, np.array([]), (0, 0, 255),
    cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

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

注意:如果您希望使用此参数并且不以段错误结束,则必须使用True作为params.filterByArea