加速HoughCircles [英] Speeding up HoughCircles

查看:111
本文介绍了加速HoughCircles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于霍夫圆函数,我对它的处理速度有疑问.当我尝试使用此视频文件运行代码时,我上传到了YouTube. https://youtu.be/5RGRTPEdHVY

I have problem with the houghcircles function, regarding its processing speed. when i try to run the code with this video file, i uploaded onto Youtube. https://youtu.be/5RGRTPEdHVY

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

The problem is, when the copter is taking off, the code is stucked, and stop moving. At this stage, when the copter is taking off, the param must be set manually at 300 to avoid being stucked. then after taking off, the param must be reduced to about 150 or 130 to detect the circles on the field.

现在我不想手动进行操作,有没有办法智能地进行操作?

Now i do not want to do it manually, is there a way to do it intelligently?

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

Or is there any way to ensure the video is smooth? are my parameters too small? And what is the unit for these parameters?

我还想知道是因为霍夫圆环在减慢速度还是画圆了?

Also i wish to know if it is the Hough Circle that is slowing things down or the drawing of the circles?

谢谢

已附加完整代码.

#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个圆.

I tried limiting the number of circles being drawn suggested by Miki (Thank you). The frame still stops at take off, although it is only drawing 2 circles and maybe 5 circles on the next frame.

如果您有一些想法,请发表评论.

Please do comment if you have some methods in mind.

推荐答案

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

I recently use Houghcircle function. I have a little experience. You declare circles(variable) that is global variable, and if you move copter but HoughCircle fcn haven't found the circle yet. It will remain circles values (center(x,y),radius) in the past, so the image looks like some latency. You can write about Hough's program as a subroutine. vector circles variable that is local variable in this subroutine. May improve latency.

这篇关于加速HoughCircles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆