加速HoughCircles

时间:2015-10-14 08:44:29

标签: c++ performance opencv hough-transform

我的houghcircles功能有问题,关于它的处理速度。当我尝试使用此视频文件运行代码时,我上传到Youtube。 https://youtu.be/5RGRTPEdHVY

问题是,当直升机起飞时,代码被卡住,并停止移动。在此阶段,当直升机起飞时,必须手动设置参数300以避免被卡住。起飞后,必须将参数减少到大约150或130以检测场上的圆圈。

现在我不想手动完成,有没有办法聪明地做到这一点?

或者有什么方法可以确保视频流畅吗? 我的参数太小了吗?这些参数的单位是什么?

另外我想知道Hough Circle是否会减慢速度或绘制圆圈?

谢谢

附上完整代码。

#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdlib.h>
#include <stdio.h>
#include <opencv2\opencv.hpp>
#include <time.h>
#include <fstream>

#include <stdio.h>
#include <conio.h>
#include "tserial.h"
#include "bot_control.h"


using namespace std;
using namespace cv;


int main(int argc, char** argv) {


    //Create a window for trackbars
    namedWindow("Trackbar Window", CV_WINDOW_AUTOSIZE);

    //Create trackbar to change brightness
    int iSliderValue1 = 50;
    createTrackbar("Brightness", "Trackbar Window", &iSliderValue1, 100);

    //Create trackbar to change contrast
    int iSliderValue2 = 50;
    createTrackbar("Contrast", "Trackbar Window", &iSliderValue2, 100);

    //Create trackbar to change param1 in HoughCircle
    int param1 = 300;
    createTrackbar("param1", "Trackbar Window", &param1, 300);

    //Create trackbar to change param2 in HoughCircle
    int param2 = 300;
    createTrackbar("param2", "Trackbar Window", &param2, 300);

    //Create trackbar to change min Radius in HoughCircle
    int minR = 0;
    createTrackbar("minRadius", "Trackbar Window", &minR, 300);

    //Create trackbar to change max Radius in HoughCircle
    int maxR = 0;
    createTrackbar("maxRadius", "Trackbar Window", &maxR, 300);

    //Debugging purpose
    cout << "All trackbars created" << endl;

    int iBrightness;
    double dContrast;

    double dparam1;
    double dparam2;

    time_t start, end;
    int counter = 0;

    ofstream myfile;

    serial comm;
    char data = 1;

    comm.startDevice("COM3", 9600);

    time(&start);   
    //Create a variable to store image
    Mat src;
    //Create video capture
    VideoCapture capture;
    //open video from either a file or webcam

    capture.open("C:\\Users\\Student-ID\\Downloads\\GOPR0506.MP4");
    //capture.open(0);



    //set frame height
    //capture.set(CV_CAP_PROP_FRAME_HEIGHT, 120);
    //capture.set(CV_CAP_PROP_FRAME_WIDTH, 120);

    //Set the capture FPS
    //capture.set(CV_CAP_PROP_FPS,25);

    //store whatever is captured to src
    capture.read(src);

    //Debugging purpose
    cout << "Vidoe opened" << endl;

    if (!src.data) {
        std::cout << "ERROR:\topening image" << std::endl;
        return -1;
    }

    //Create window to display videos
    cv::namedWindow("image1", CV_WINDOW_AUTOSIZE);

    vector<Vec3f> circles;
    while (true){

        capture.read(src);
        //Code for changing Brightness and contrast
        iBrightness = iSliderValue1 - 50;
        dContrast = iSliderValue2 / 50.0;
        src.convertTo(src, -1, dContrast, iBrightness);

        //Debugging purpose
        //cout << "1" << endl;


        //Create variable to store the processed image
        Mat src_gray2;
        //Convert the colour to grayscale
        cvtColor(src, src_gray2, CV_BGR2GRAY);
        //Smooth and blur the image to reduce noise
        GaussianBlur(src_gray2, src_gray2, cv::Size(9, 9), 2, 2);





        //Change the param1 and 2 to double from integer
        dparam1 = param1 / 1.0;
        dparam2 = param2 / 1.0;

        //Debugging purpose
        cout << "2" << endl;

        //Apply HoughCircle function
        HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT,
        2,   // accumulator resolution (size of the image / 2)
        5,  // minimum distance between two circles
        dparam1, // Canny high threshold
        dparam2, // minimum number of votes
        minR, maxR); // min and max radius

        //Debugging purpose
        cout << "3" << endl;

        cout << "size of circle is: "<< circles.size() << endl;


        if (circles.size() != 0){
            //Draw the circle
            for (size_t i = 0; i < circles.size(); i++)
            {
                Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
                int radius = cvRound(circles[i][2]);
                circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
                // circle outline
                circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
                //Display words on top left hand corner
                putText(src, "Circle Found", Point(0, 50), 1, 2, Scalar(0, 255, 0), 2);

                comm.send_data(data);
            }



        }



        //display video

        imshow("image1", src);

        //debugging purpose
        cout << "5" << endl;

        //delay to refresh the pic
        int key = cvWaitKey(33);
        if (key == 27) break;

        time(&end);
        ++counter;
        cout << "fps is: " << counter / difftime(end, start) << endl << endl;
    }
    return 0;
}
编辑:我尝试限制Miki建议的圈子数量(谢谢)。 虽然在下一帧上只绘制了2个圆圈和5个圆圈,但框架仍然在起飞时停止。

如果您有一些方法,请发表评论。

1 个答案:

答案 0 :(得分:0)

我最近使用了Houghcircle函数。我有一点经验。您声明是全局变量的圆(变量),如果您移动直升飞机,但HoughCircle fcn尚未找到该圆。它将保留过去的圆圈值(center(x,y),radius),因此图像看起来有些延迟。您可以将Hough程序作为子例程编写。向量圈变量,它是此子例程中的局部变量。可能会改善延迟。