【发布时间】:2015-01-06 18:20:28
【问题描述】:
我一直在尝试检测图像上的一些标记。我使用 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 解决方案?
【问题讨论】:
-
如果没有示例图像,就不可能回答这个问题。使用免费的图片共享服务上传一些示例(因为您没有足够的声誉来为您的问题添加图片)。
-
您的目标图像中显然有模型,但它有很多失真。 MatchTemplate 将无法为您提供帮助。您似乎是图像处理的新手,我建议您在继续练习之前多阅读一些有关该主题的内容并练习更简单的问题。
标签: c++ opencv visual-studio-2013 computer-vision