C++ 无法通过在 OpenCV 中使用 SimpleBlobDetection 来检测乐谱上的省略号

C++ Unable to detect ellipses on music staff by using SimpleBlobDetection in OpenCV

我想使用 SimpleBlobDetection 检测乐谱上的 circles/ellipses,但是当我尝试检测它们时,它在图片上发现了不相关的点。

原图:

Blob 检测后:

请看下面的代码:

cv::SimpleBlobDetector::Params params;

//Thresholds
params.minThreshold = 10;
params.maxThreshold = 200;

// Filter by Area
params.filterByArea = true;
params.minArea = 100;
params.maxArea = 500;

// Filter by Circularity
params.filterByCircularity = true;
params.minCircularity = 0.1;
params.maxCircularity = 0.5;

// Filter by Convexity
params.filterByConvexity = true;
params.minConvexity = 0.57;
params.maxConvexity = 0.97;

// Filter by Inertia
params.filterByInertia = true;
params.minInertiaRatio = 0.01;

// set up and create the detector using the parameters
cv::SimpleBlobDetector blob_detector(params);

// detect!
vector<cv::KeyPoint> keypoints;
blob_detector.detect(tresh, keypoints);

// extract the x y coordinates of the keypoints: 
for (int i = 0; i < keypoints.size(); i++){
    float X = keypoints[i].pt.x;
    float Y = keypoints[i].pt.y;
    circle(tresh, Point(X, Y), 1, Scalar(0, 255, 0), 3, CV_AA);
}

imshow("Detected Blobs", tresh);

请帮帮我...

Hough transform 检测圆怎么样?

您是否尝试过不同的过滤值(尤其是 min/max 面积 - 这些是平方值)?另外,按颜色(即仅白色)应用过滤怎么样?

这是解决方案。我从 SimpleBlobDetector Tutorial 复制过来的

# Standard imports
import cv2
import numpy as np;

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

_, im = cv2.threshold(im, 128, 255, cv2.THRESH_BINARY)

im = 255 - im; 
im = 255 - cv2.erode(im, np.ones((3,3)), iterations=2)

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

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

params.filterByConvexity = False

# 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(im)

# Draw blobs
im_with_keypoints = cv2.drawKeypoints(im_orig, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

#Write image
cv2.imwrite("treble_staff.jpg", im_with_keypoints)

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