基于OpenCV的标志点检测

来源:互联网 发布:2015大麦户源码 编辑:程序博客网 时间:2024/06/11 00:03

机器视觉中所用的标志点一般为圆形,圆形标志点会因拍摄角度的问题而在图像中呈现椭圆形,因此标志点检测一般是指椭圆检测,目的是获得标志点圆心坐标。OpenCV实现如下,编程环境为Qt5.3.2.

需要包含的OpenCV头文件如下:

#include <opencv/cv.h>#include <opencv/highgui.h>#include <opencv2/imgproc/imgproc.hpp>#include <math.h>

源文件内容:

int sliderPos = 60;//图像二值化阈值int sizeLimit = 8;//设置检出圆的大小下限,小于此值的圆认为不是标志点,过滤掉Mat image;void processImage(int, void*);HWidget::HWidget(QWidget *parent)    : QWidget(parent){    image = imread("D:/1.png",0);    imshow("source", image);    namedWindow("result", WINDOW_AUTOSIZE);    // Create toolbars. HighGUI use.    createTrackbar( "threshold", "result", &sliderPos, 255, processImage );    createTrackbar( "sizehold", "result", &sizeLimit, 20, processImage );    processImage(0, 0);    // Wait for a key stroke; the same function arranges events processing    waitKey();    }

// Define trackbar callback functon. This function find contours,// draw it and approximate it by ellipses.void processImage(int /*h*/, void*){    vector<vector<Point> > contours;    Mat bimage = image >= sliderPos;    findContours(bimage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);    Mat cimage = Mat::zeros(bimage.size(), CV_8UC3);    for(size_t i = 0; i < contours.size(); i++)    {        size_t count = contours[i].size();        if( count < 6 )            continue;        Mat pointsf;        Mat(contours[i]).convertTo(pointsf, CV_32F);        RotatedRect box = fitEllipse(pointsf);        if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*1.5 )            continue;        if(box.size.width <= sizeLimit || box.size.height <= sizeLimit)            continue;        if(box.size.width > 30 || box.size.height > 30)            continue;        drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8);        ellipse(cimage, box, Scalar(0,0,255), 1, CV_AA);        ellipse(cimage, box.center, box.size*0.5f, box.angle, 0, 360, Scalar(0,255,255), 1, CV_AA);        Point2f vtx[4];        box.points(vtx);        for( int j = 0; j < 4; j++ )            line(image, vtx[j], vtx[(j+1)%4], Scalar(0,255,0), 1, CV_AA);    }    imshow("result", cimage);}


int main(int argc, char *argv[]){    QApplication a(argc, argv);    HWidget w;    return a.exec();}

程序检测结果:



0 0
原创粉丝点击