检测图像上标记的最佳方法是什么?开放简历

What is the best way to detect mark on an image? OpenCV

本文关键字:是什么 方法 上标 图像 最佳 检测      更新时间:2023-10-16

我一直在尝试检测图像上的一些标记。我使用 OpenCV 的 matchShapes 和 matchTemplate 函数来检测符号"X"、"O"、"+",甚至是三角形,但我遇到了太小的精度:(有时它会正确检测。有时它会检测图像的小片段,例如小点或破折号,而不是三角形或"X"符号。

以下是源代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <limits>
using namespace cv;
int main(int argc, char** argv)
{
    Mat src;
    Mat sample;
    Mat circle;
    int idx = 0;
    int ind_min;
    double ret;
    double min = std::numeric_limits<double>::max();
    src = imread("triangle.jpg", 0); // Example
    sample = imread("tri4.jpg", 0); // Photo to compare with example
    adaptiveThreshold(sample, sample, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY_INV, 51, 10);
    threshold(src, src, 140, 255, 1);
    Mat dst2 = Mat::zeros(sample.rows, sample.cols, CV_8UC3);
    src = src > 1;
    sample = sample > 1;
    namedWindow("Source", 1);
    imshow("Source", src);
    namedWindow("Sample", 1);
    imshow("Sample", sample);
    vector<vector<Point> > contours; //Contour of an example
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours2; //Contours of a photo
    vector<Vec4i> hierarchy2;
    findContours(src, contours, hierarchy,
        CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    findContours(sample, contours2, hierarchy2,
        CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    //Matching example contours[0] with contours of the photo contours2[idx].
    //Comparing output of matchShapes function, the lower is better. 
    for (; idx >= 0; idx = hierarchy2[idx][0])
    {
        ret = matchShapes(contours[0], contours2[idx], CV_CONTOURS_MATCH_I1, 0.0);
        if (ret < min && ret > 0)
        {
            min = ret;
            ind_min = idx;
        }
    }
    Scalar color(rand() & 255, rand() & 255, rand() & 255);
    drawContours(dst2, contours2, ind_min, color, CV_FILLED, 8, hierarchy2);
    namedWindow("Components", 1);
    imshow("Components", dst2);
    waitKey(0);
}
检测

为标记的最佳符号是什么?什么是最简单的具有良好准确性的 opencv 解决方案?

如果精度是一个问题,请确保标记的比例和投影与要比较的图像足够接近。您可以缩放图像以帮助匹配。

使用像SIFT(或SURF)这样的特征检测器和像FLANN(甚至蛮力)这样的特征匹配器来获得最佳结果。
此外,尝试尽可能降低噪声,以获得更准确的特征检测(高斯滤波器或去噪函数)。