加快HoughCircles的处理速度

时间:2015-10-03 09:01:29

标签: c++ performance opencv hough-transform

我正在研究Hough Circles的功能。 当我使用此功能时,处理速度非常慢。例如,当我移动相机时,实时视频输入将具有1秒的滞后。甚至10秒。 代码如下

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

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 = 150;
    createTrackbar("param1", "Trackbar Window", &param1, 300);

    //Create trackbar to change param2 in HoughCircle
    int param2 = 200;
    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;

    //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\\SPACe Mission IIIA\\GOPR0503.mp4");
    capture.open(0);

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

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

    //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);


    while (true){

        capture.read(src);
        //Code for changing Brightness and contrast
        int iBrightness = iSliderValue1 - 50;
        double 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);

        vector<Vec3f> circles;



        //Change the param1 and 2 to double from integer
        double dparam1 = param1 / 1.0;
        double 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;




        //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);
        }
        //display video

        imshow("image1", src);

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

        //delay to refresh the pic
        cvWaitKey(33);

    }
    return 0;
}

因此调试编号在跳转到3之前的10秒内停止在“2”。 我之前被告知要增加param1和param2,但如果我最大为300或甚至200,则不会检测到任何圆圈。 注意:我的圈子有各种尺寸,所以我们可以忘记最小和最大半径

这个滞后量对我来说太过分了,有没有办法提高编码和其他处理的速度?

我在Microsoft Visual Studio 2013上使用OPENCV 3.0.0 C ++,运行Win 8 64位系统。

0 个答案:

没有答案