Как точно определить треугольник в OpenCv с помощью C ++ - PullRequest
1 голос
/ 04 мая 2019

Я обнаруживаю объекты красного треугольника в видео в реальном времени, используя OpenCv c ++. Но моя программа не работает должным образом. Не могу ли я использовать findContours или окPolyDP не очень хорошо. Вот мой код


#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

Это изображения, но они не могут обнаружить треугольники и сбить с толку.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...