从音符 Mat/image 检测 OpenCV 符头

OpenCV Notehead detection from note Mat/image

我正在做一个项目,我需要从五线谱线中检测音乐元素,我现在知道 duration 音符元素具有 (quarter, octet, etc) 然后我要检测 note-head 的中心,这样我就可以找出 note 是什么 (C, D, etc)基于它在谱线上的位置。

我的问题是我不知道从哪里开始。 我在考虑一些 template-matching 使用完整和空椭圆作为模板和元素 Mat 作为源。

有没有人有更好的最优解?

我想从中找到元素垫的示例 note-head:

项目 GitHub 如果有人感兴趣 https://github.com/AmbroziePaval/OMR

使用一次一个元素(注释)的模板匹配实现。

示例搜索所有区域并用绿色绘制中心点。

代码:

public Point getAproximateCenterNoteHeadPoint(Mat noteMat) {
    noteMat.convertTo(noteMat, CvType.CV_32FC1);

    Mat fullNoteHeadMat = Imgcodecs.imread(DatasetPaths.FULL_HEAD_TEMPLATE.getPath());
    if (fullNoteHeadMat.channels() == 3) {
        Imgproc.cvtColor(fullNoteHeadMat, fullNoteHeadMat, Imgproc.COLOR_BGR2GRAY);
    }
    fullNoteHeadMat.convertTo(fullNoteHeadMat, CvType.CV_32FC1);

    Mat result = new Mat();
    result.create(noteMat.width(), noteMat.height(), CvType.CV_32FC1);
    double threshold = 0.7;

    Imgproc.matchTemplate(noteMat, fullNoteHeadMat, result, Imgproc.TM_CCOEFF_NORMED);
    Imgproc.threshold(result, result, threshold, 255, Imgproc.THRESH_TOZERO);

    Core.MinMaxLocResult minMaxLocResult = Core.minMaxLoc(result);
    if (minMaxLocResult.maxVal > threshold) {
        Point maxLoc = minMaxLocResult.maxLoc;
        return new Point(maxLoc.x + fullNoteHeadMat.width() / 2, maxLoc.y + fullNoteHeadMat.height() / 2);
    }
    return null;
}

如@Alexander Reynolds 在问题的评论中建议的那样,一次对所有元素使用模板匹配的实现:

public List<Point> findAllNoteHeadCenters(Mat imageMat, List<Rect> elementRectangles) {
    imageMat.convertTo(imageMat, CvType.CV_32FC1);

    Mat fullNoteHeadMat = Imgcodecs.imread(DatasetPaths.FULL_HEAD_TEMPLATE.getPath());
    if (fullNoteHeadMat.channels() == 3) {
        Imgproc.cvtColor(fullNoteHeadMat, fullNoteHeadMat, Imgproc.COLOR_BGR2GRAY);
    }
    fullNoteHeadMat.convertTo(fullNoteHeadMat, CvType.CV_32FC1);

    Mat result = new Mat();
    result.create(imageMat.width(), imageMat.height(), CvType.CV_32FC1);
    double threshold = 0.75;

    Imgproc.matchTemplate(imageMat, fullNoteHeadMat, result, Imgproc.TM_CCOEFF_NORMED);
    Imgproc.threshold(result, result, threshold, 255, Imgproc.THRESH_TOZERO);

    List<Point> centers = new ArrayList<>();
    Set<Rect> foundCenterFor = new HashSet<>();

    while (true) {
        Core.MinMaxLocResult minMaxLocResult = Core.minMaxLoc(result);
        if (minMaxLocResult.maxVal > threshold) {
            Point maxLoc = minMaxLocResult.maxLoc;
            Optional<Rect> containingRect = getPointContainingRect(maxLoc, elementRectangles);

            if (containingRect.isPresent() && !foundCenterFor.contains(containingRect.get())) {
                centers.add(new Point(maxLoc.x + fullNoteHeadMat.width() / 2, maxLoc.y + fullNoteHeadMat.height() / 2));
                foundCenterFor.add(containingRect.get());
            }
            Imgproc.floodFill(result, new Mat(), minMaxLocResult.maxLoc, new Scalar(0));
        } else {
            break;
        }
    }
    return centers;
}

尝试使用 Chamfer based distance transform 变换来找到您的点的中心。该算法将图像传递 2 次以计算每个对象像素到最近边缘的距离。对象的中心点将是指定距离最大的那个。