如何使用C ++在OpenCv中准确检测三角形

时间:2019-05-04 15:36:48

标签: c++ opencv

我正在使用OpenCv c ++在实时视频中检测红色三角形对象。但是我的程序无法正常运行。我是否不能很好地使用findContours或roxPolyDP。这是我的代码


#include "opencv2/opencv.hpp" 
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <raspicam/raspicam_cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>


using namespace cv;
using namespace std;
int thresh = 100;
int number_pixels;
void drawAllTriangles(Mat&, const vector< vector<Point> >&);
int main(){

    //Vectors
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    Mat canny_output,drawing;
    //Initialise the image as a matrix container
    Mat bgr;
    raspicam::RaspiCam_Cv capture; // initialise the raspicam object

    capture.open(); // activate the raspicam object

    while (1)
    {
        capture.grab(); //grab the scene using raspicam
        capture.retrieve(bgr); // retrieve the captured scene as an image and store it in matrix container
        Mat gray,hsv; //Initialise the matrix container for gray color image
        resize(bgr, bgr, Size(), .25, 0.25, CV_INTER_AREA);
        //cvtColor(bgr, gray, COLOR_BGR2GRAY); //OpenCV code line for converting COLOR to GRAY scale image


        //
        cvtColor(bgr, hsv, COLOR_BGR2HSV);

        //Gaussian Noice
        Mat blure;
        GaussianBlur(hsv,blure,Size(9,9),1.0);
        //Gaussian Noice End

        Mat mask1, mask2;    
            inRange(blure, Scalar(0,70,50), Scalar(10, 255, 255), mask1);
            inRange(blure, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);

            Mat mask3 = mask1 | mask2;
            //Mat mask3 = mask1;

        //
        number_pixels = countNonZero(mask3);

        Canny(mask3, canny_output, thresh, thresh*2, 3 );

        findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
        drawing = Mat::zeros( canny_output.size(), CV_8UC3 );

        imshow("Original frame", bgr); //displaying original frame
        imshow("Gray frame", mask3); //displaying original frame

        drawAllTriangles(drawing,contours);
        imshow("Triangles",drawing);

        if (cvWaitKey(20) == 'q') // waitkey 
            break;
    }
    capture.release(); // release the raspicam frame grabbing 
    return 0;
}

void drawAllTriangles(Mat& img, const vector< vector<Point> >& contours){
    vector<Point> approxTriangle;
    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 and number_pixels>200){
            drawContours(img, contours, i, Scalar(0, 255, 255), CV_FILLED); // fill GREEN
            vector<Point>::iterator vertex;
            for(vertex = approxTriangle.begin(); vertex != approxTriangle.end(); ++vertex){
                circle(img, *vertex, 3, Scalar(0, 0, 255), 1);
            }
            printf("Triangle \n");
        }

    else {
        printf("None \n");
    }


    }
}

我的程序应该检测视频中的任何红色三角形对象。我要去哪里错了。谢谢你的回答

enter image description here

enter image description here

这些是图像,但无法检测到三角形和令人困惑的地方。

0 个答案:

没有答案