mev*_*ron 10
这是一个简单的matchTemplate解决方案,类似于Guy Sirton提到的方法.
只要您的目标没有太多缩放或旋转,模板匹配就会起作用.
这是我使用的模板:

这是我用来检测几个无障碍十字架的代码:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
string inputName = "crosses.jpg";
string outputName = "crosses_detect.png";
Mat img = imread( inputName, 1);
Mat templ = imread( "crosses-template.jpg", 1);
int resultCols = img.cols - templ.cols + 1;
int resultRows = img.rows - templ.rows + 1;
Mat result( resultCols, resultRows, CV_32FC1 );
matchTemplate(img, templ, result, CV_TM_CCOEFF);
normalize(result, result, 0, 255.0, NORM_MINMAX, CV_8UC1, Mat());
Mat resultMask;
threshold(result, resultMask, 180.0, 255.0, THRESH_BINARY);
Mat temp = resultMask.clone();
vector< vector<Point> > contours;
findContours(temp, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(templ.cols / 2, templ.rows / 2));
vector< vector<Point> >::iterator i;
for(i = contours.begin(); i != contours.end(); i++)
{
Moments m = moments(*i, false);
Point2f centroid(m.m10 / m.m00, m.m01 / m.m00);
circle(img, centroid, 3, Scalar(0, 255, 0), 3);
}
imshow("img", img);
imshow("results", result);
imshow("resultMask", resultMask);
imwrite(outputName, img);
waitKey(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这导致了这个检测图像:

该代码基本上设置了一个阈值,用于将交叉峰与图像的其余部分分开,然后检测所有这些轮廓.最后,它计算每个轮廓的质心以检测十字的中心.
这是使用三角形检测的替代方法.它看起来不像matchTemplate方法那么准确,但可能是你可以使用的另一种选择.
使用findContours我们检测图像中的所有三角形,结果如下:

然后我注意到所有三角形顶点聚集在交叉中心附近,因此这些聚类用于质心下面显示的交叉中心点:

最后,这是我用来执行此操作的代码:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <list>
using namespace cv;
using namespace std;
vector<Point> getAllTriangleVertices(Mat& img, const vector< vector<Point> >& contours);
double euclideanDist(Point a, Point b);
vector< vector<Point> > groupPointsWithinRadius(vector<Point>& points, double radius);
void printPointVector(const vector<Point>& points);
Point computeClusterAverage(const vector<Point>& cluster);
int main(int argc, char* argv[])
{
Mat img = imread("crosses.jpg", 1);
double resizeFactor = 0.5;
resize(img, img, Size(0, 0), resizeFactor, resizeFactor);
Mat momentImg = img.clone();
Mat gray;
cvtColor(img, gray, CV_BGR2GRAY);
adaptiveThreshold(gray, gray, 255.0, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 19, 15);
imshow("threshold", gray);
waitKey();
vector< vector<Point> > contours;
findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
vector<Point> allTriangleVertices = getAllTriangleVertices(img, contours);
imshow("img", img);
imwrite("shape_detect.jpg", img);
waitKey();
printPointVector(allTriangleVertices);
vector< vector<Point> > clusters = groupPointsWithinRadius(allTriangleVertices, 10.0*resizeFactor);
cout << "Number of clusters: " << clusters.size() << endl;
vector< vector<Point> >::iterator cluster;
for(cluster = clusters.begin(); cluster != clusters.end(); ++cluster)
{
printPointVector(*cluster);
Point clusterAvg = computeClusterAverage(*cluster);
circle(momentImg, clusterAvg, 3, Scalar(0, 255, 0), CV_FILLED);
}
imshow("momentImg", momentImg);
imwrite("centroids.jpg", momentImg);
waitKey();
return 0;
}
vector<Point> getAllTriangleVertices(Mat& img, const vector< vector<Point> >& contours)
{
vector<Point> approxTriangle;
vector<Point> allTriangleVertices;
for(size_t i = 0; i < contours.size(); i++)
{
approxPolyDP(contours[i], approxTriangle, arcLength(Mat(contours[i]), true)*0.05, true);
if(approxTriangle.size() == 3)
{
copy(approxTriangle.begin(), approxTriangle.end(), back_inserter(allTriangleVertices));
drawContours(img, contours, i, Scalar(0, 255, 0), CV_FILLED);
vector<Point>::iterator vertex;
for(vertex = approxTriangle.begin(); vertex != approxTriangle.end(); ++vertex)
{
circle(img, *vertex, 3, Scalar(0, 0, 255), 1);
}
}
}
return allTriangleVertices;
}
double euclideanDist(Point a, Point b)
{
Point c = a - b;
return cv::sqrt(c.x*c.x + c.y*c.y);
}
vector< vector<Point> > groupPointsWithinRadius(vector<Point>& points, double radius)
{
vector< vector<Point> > clusters;
vector<Point>::iterator i;
for(i = points.begin(); i != points.end();)
{
vector<Point> subCluster;
subCluster.push_back(*i);
vector<Point>::iterator j;
for(j = points.begin(); j != points.end(); )
{
if(j != i && euclideanDist(*i, *j) < radius)
{
subCluster.push_back(*j);
j = points.erase(j);
}
else
{
++j;
}
}
if(subCluster.size() > 1)
{
clusters.push_back(subCluster);
}
i = points.erase(i);
}
return clusters;
}
Point computeClusterAverage(const vector<Point>& cluster)
{
Point2d sum;
vector<Point>::const_iterator point;
for(point = cluster.begin(); point != cluster.end(); ++point)
{
sum.x += point->x;
sum.y += point->y;
}
sum.x /= (double)cluster.size();
sum.y /= (double)cluster.size();
return Point(cvRound(sum.x), cvRound(sum.y));
}
void printPointVector(const vector<Point>& points)
{
vector<Point>::const_iterator point;
for(point = points.begin(); point != points.end(); ++point)
{
cout << "(" << point->x << ", " << point->y << ")";
if(point + 1 != points.end())
{
cout << ", ";
}
}
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
我在之前的实现中修复了一些错误,并稍微清理了一下代码.我还用各种调整大小的因素测试了它,它似乎表现得相当好.然而,在我达到四分之一刻度后,它开始难以正确检测三角形,因此这对于极小的十字架可能不适用.此外,似乎moments函数中存在一个错误,因为它返回的某些有效集群(-NaN,-NaN)位置.所以,我相信准确性会有所改善.它可能需要进行一些调整,但总的来说,我认为它应该是一个很好的起点.
我认为如果三角形周围的黑色边框更粗/更清晰,并且三角形本身的阴影更少,我的三角形检测效果会更好.
希望有所帮助!