C ++在OpenCV中使用SimpleBlobDetection无法检测音乐人员的椭圆

时间:2015-04-07 08:51:42

标签: c++ opencv image-processing image-recognition

我想通过使用SimpleBlobDetection检测音乐人员的圆圈/椭圆,但是当我尝试检测它们时,它会在图片上找到不相关的点。

原始图片:

enter image description here

Blob检测后:

enter image description here

请参阅以下代码:

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);

请帮帮我......

3 个答案:

答案 0 :(得分:0)

如何使用Hough transform检测圈子?

答案 1 :(得分:0)

您是否尝试过不同的过滤器值(尤其是最小/最大区域 - 这些是平方值)?那么,应用颜色过滤(即仅白色)呢?

答案 2 :(得分:0)

这是解决方案。我从SimpleBlobDetector Tutorial复制了它 Treble Staff Detector

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