C++ 无法通过在 OpenCV 中使用 SimpleBlobDetection 检测乐谱上的椭圆

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

请帮帮我...

最佳答案

这是解决方案。我是从 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)

关于C++ 无法通过在 OpenCV 中使用 SimpleBlobDetection 检测乐谱上的椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29487509/

相关文章:

c++ - 尝试查找数组中点之间的最小距离时随机垃圾输出

用于 3D Lucas Kanade 的 OpenCV?

python - 如何检测图像上的对象?

python - 如何在 Python 中使用 OpenCV 3.0 中的 HOG 功能训练 SVM 分类器?

c# - 如何提高网络摄像头视频输入的帧率?埃姆古简历

c++ - 在构造函数代码之前禁用默认类成员初始化

c++ - 我们如何注册具有特定文件扩展名的 Shell 扩展

c++ - Steam API 获取角色名称

Python - 使用 OpenCV 将图像保存在文件中后,窗口中的图像外观有所不同

image-processing - DICOM 压缩显式 VR 小端 (1.2.840.10008.1.2.1.99)