我一直试图在图像上检测到一些标记。我使用了OpenSV的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解决方案具有良好的准确性?
答案 0 :(得分:2)
如果准确性是个问题,请确保标记的比例和投影足够接近您想要比较的图像。您可以缩放图像以帮助匹配。
使用 SIFT (或SURF)等功能检测器和 FLANN (甚至是暴力)等功能匹配器来获得最佳效果。
此外,尝试尽可能降低噪声以获得更准确的特征检测(高斯滤波器或去噪函数)。