c - 检测图像中的十字

标签 c image-processing opencv ffmpeg

我正在开发一个程序来检测探测设备的尖端并分析探测过程中的颜色变化。输入/输出机制或多或少已经到位。我现在需要的是真正的内容:检测提示。

在下图中,尖端位于十字的中心。我想在经过一些阈值处理后将 BFS 应用于图像,但后来卡住了,不知道如何继续。在阅读到它提供图像特征检测后,我转向了 OpenCV。然而,我对这里一次又一次使用的大量概念和技术感到不知所措,不知道如何进行。

我的看法是否正确?你能给我一些建议吗?

Colored Image 从短视频中提取的图像

Binary image with threshold set at 95 阈值设置为 95 的二进制版本

最佳答案

模板匹配方法

这是一个简单的 matchTemplate解决方案,这类似于 Guy Sirton 提到的方法。

只要您的目标没有发生太多缩放或旋转,模板匹配就会起作用。

这是我使用的模板: enter image description here

这是我用来检测几个无障碍十字的代码:

#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;
}

这导致了这个检测图像:
enter image description here

这段代码基本上设置了一个阈值,将交叉峰与图像的其余部分分开,然后检测所有这些轮廓。最后,它计算每个轮廓的质心以检测十字的中心。

形状检测替代方案

这是使用三角形检测的替代方法。它似乎不如 matchTemplate 方法准确,但可能是您可以尝试的替代方法。

我们使用 findContours 检测图像中的所有三角形,结果如下:

enter image description here

然后我注意到所有的三角形顶点都聚集在交叉中心附近,所以这些簇被用来对交叉中心点进行质心,如下所示:

enter image description here

最后,这是我用来执行此操作的代码:

#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;
}

我修复了之前实现中的一些错误,并稍微清理了代码。我还使用各种调整大小因素对其进行了测试,它似乎表现得相当不错。然而,在我达到四分之一比例后,它开始无法正确检测三角形,因此这对于极小的十字可能效果不佳。此外,moments 函数中似乎存在错误,因为它返回 (-NaN, -NaN) 位置的某些有效集群。因此,我相信准确性会有所提高。它可能需要再进行一些调整,但总的来说,我认为这对您来说应该是一个很好的起点。

我认为,如果三角形周围的黑色边框更厚/更锐利一点,并且三角形本身的阴影更少,我的三角形检测效果会更好。

希望对您有所帮助!

关于c - 检测图像中的十字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9854384/

相关文章:

qt - 整合QT和OpenCV?

c - 创建私有(private)枚举值

c++ - 如何在 Windows 中制作一个简单的 Hello World "invisible"(C/C++)

c++ - 为 C++ 库编写 C 包装器?

c++ - Opencv 数据类型定义

algorithm - cv::undistortPoints() - 迭代算法解释

android - 如何使用 onPreviewFrame() 中的 byte[] 数据在肖像中使用 OpenCV 人脸检测?

c - 在c中加载位图字体

image-processing - 用于分类的词袋 - 特征与像素

r - videoplayR 的 github_install 和 R CMD INSTALL 失败