1

I'm trying to detect a whole note and a half note, but for the half note, it seems that I couldn't detect it, as it is a hollowed circle. Is there a way to detect the hollowed circle?

Example: enter image description here

Here is my code:

#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{

    // Read image
    Mat im = imread("beethoven_ode_to_joy.jpg", IMREAD_GRAYSCALE);

    // Setup SimpleBlobDetector parameters.
    SimpleBlobDetector::Params params;

    // Change thresholds
    params.minThreshold = 10;
    params.maxThreshold = 200;

    // Filter by Area.
    params.filterByArea = true;
    params.minArea = 15;

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

    // Filter by Convexity
    params.filterByConvexity = true;
    params.minConvexity = 0.01;

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


    // Storage for blobs
    vector<KeyPoint> keypoints;


#if CV_MAJOR_VERSION < 3   // If you are using OpenCV 2

    // Set up detector with params
    SimpleBlobDetector detector(params);

    // Detect blobs
    detector.detect(im, keypoints);
#else 

    // Set up detector with params
    Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);

    // Detect blobs
    detector->detect(im, keypoints);
#endif 

    // Draw detected blobs as red circles.
    // DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures
    // the size of the circle corresponds to the size of blob

    Mat im_with_keypoints;
    drawKeypoints(im, keypoints, im_with_keypoints, Scalar(0, 0, 255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

    // Show blobs
    imshow("keypoints", im_with_keypoints);
    imwrite("a.jpg", im_with_keypoints);
    waitKey(0);

}
Patrick Yu
  • 972
  • 1
  • 7
  • 19
Jon
  • 35
  • 6

3 Answers3

1

My suggestion to you is to use some machine learning algorithms. Here's the whole idea in a nutshell: You first need to create a training set for the images. In the training set you need to label a few things. One label is "hollowed circle". Then you label other notes. I don't know how many musical notes there are, but you may label each one separately or label all musical notes that are not hallowed circle as one thing. You may also label the background. Then you train a machine learning model over your training data and then feed your test data (the images that the model has not seen while training) into it and get an accuracy. You may split your data into training and validation sets for training.

For labelling you may use this website.

Community
  • 1
  • 1
Amir
  • 10,600
  • 9
  • 48
  • 75
1

There are different ways to do that. Here is a simple one:

  • First (optional), I would use the Hough transform in order to detect the partition, not the notes, because it's often easier to work on a simpler image. You know exactly the number of vertical/horizontal lines you have to find, so it's easy to parametrize the Hough transform. IF your image is perfectly scanned, you can also use a histogram projection (also known as integral projection function).
  • As some notes are not totally full, I would start by a little FillHole operation (see here or here). It can also be done using a closing. If you don't do it, you segment only the whole notes (see result below).
  • Now, you perform a small opening, and it will only remain the keys and the notes.

Results:

  • Opening result without the filling.
  • Filling result.
  • Final result (filling + opening). Not perfect yet, but really close to the final solution, particularly if you apply first the Hough transform, and apply my comment below.

General comment: DO NOT use JPG format, it adds a lot of artefacts, which are particularly annoying in images processing, especially when you work on so tiny patterns detection.

FiReTiTi
  • 5,597
  • 12
  • 30
  • 58
  • i have a code, but it can only detect a circle and it could't detect a hollow circle – Jon Dec 31 '15 at 04:10
  • The operations/algorithm I use should be available in OpenCV – FiReTiTi Dec 31 '15 at 07:47
  • can the blob detection detect the hollowed circle? – Jon Dec 31 '15 at 11:35
  • Probably. But I used mainly Open/Close that are present in OpenCV. – FiReTiTi Dec 31 '15 at 23:08
  • See the images I attached! The results are not perfect but really close of what you want! – FiReTiTi Jan 01 '16 at 21:52
  • may i ask what are the methods that you have use for the filling result? – Jon Jan 02 '16 at 03:33
  • I've add two links for this specific operation. – FiReTiTi Jan 02 '16 at 08:04
  • I don't think because everything is connect: partition and notes. You can apply what you've done only when the partition is no longer present – FiReTiTi Jan 02 '16 at 10:07
  • when i run this code, i can detect the circle but not all circles i dont know why it does not recognize all circle. – Jon Jan 02 '16 at 10:34
  • the circle that i dont recognize is the hollowed circle – Jon Jan 02 '16 at 10:34
  • If you perform an opening (erosion+dilation) without filling beforehand, the erosion erases everything, so there is nothing left to dilate. – FiReTiTi Jan 03 '16 at 04:44
  • so i need to fill the image before performing the opening to be able to dilate? – Jon Jan 03 '16 at 07:01
  • As I said in my solution, it would be definitely better. But I used a filling with a threshold in order to not fill the partition, only the small holes into the notes. – FiReTiTi Jan 03 '16 at 08:16
  • i see, may i ask for your help with the given code. I was trying to get the centroid of the detected circle but i dont have any idea on how to get the centroid of it. – Jon Jan 03 '16 at 08:45
  • Sorry, I don't know OpenCV. But you don't need it to perform a filling. – FiReTiTi Jan 03 '16 at 08:56
  • Ohh, Thank you very much. I will try to use the method that you have used. – Jon Jan 03 '16 at 09:05
0

Template matching can be quite general and I dont know what you mean by it.

A hollowed circle is a circle - as we call it.

So my first sggestion would be to use hough transform (whether your circles become ellipse is questionable and you can see).

Since your circles are of one size you may be able to have success with hough transform - read about it

gpasch
  • 2,672
  • 3
  • 10
  • 12
  • but i need to detect the hollowed circle, on the other hand i can already detect a normal circle. – Jon Dec 31 '15 at 04:06