C++ 加速霍格圈

C++ 加速霍格圈,c++,performance,opencv,hough-transform,C++,Performance,Opencv,Hough Transform,我对houghcircles函数的处理速度有问题。当我试图用这个视频文件运行代码时,我上传到了Youtube上 问题是,当直升机起飞时,代码被阻塞,停止移动。在这个阶段,当直升机起飞时,必须手动将参数设置为300,以避免卡住。起飞后,必须将参数降低到150或130左右,以检测场地上的圆圈 现在我不想手动操作,有没有一种智能化的方法 或者有没有办法确保视频流畅? 我的参数是否太小?这些参数的单位是多少 我还想知道是霍夫圆减慢了速度还是画出了圆 多谢各位 完整代码见附件 #include <s

我对houghcircles函数的处理速度有问题。当我试图用这个视频文件运行代码时,我上传到了Youtube上

问题是,当直升机起飞时,代码被阻塞,停止移动。在这个阶段,当直升机起飞时,必须手动将参数设置为300,以避免卡住。起飞后,必须将参数降低到150或130左右,以检测场地上的圆圈

现在我不想手动操作,有没有一种智能化的方法

或者有没有办法确保视频流畅? 我的参数是否太小?这些参数的单位是多少

我还想知道是霍夫圆减慢了速度还是画出了圆

多谢各位

完整代码见附件

#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;
}
#包括
#包括
#包括
#包括
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“tserial.h”
#包括“bot_control.h”
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv){
//为轨迹栏创建一个窗口
namedWindow(“轨迹栏窗口”,CV\u窗口\u自动调整大小);
//创建轨迹栏以更改亮度
int iSliderValue1=50;
createTrackbar(“亮度”、“轨迹栏窗口”和IsliderValue1100);
//创建轨迹栏以更改对比度
int iSliderValue2=50;
createTrackbar(“对比度”、“轨迹栏窗口”和IsliderValue2100);
//创建轨迹栏以更改HoughCircle中的参数1
int参数1=300;
createTrackbar(“参数1”、“轨迹栏窗口”和参数1300);
//创建轨迹栏以更改HoughCircle中的param2
int参数2=300;
createTrackbar(“param2”、“Trackbar窗口”和Param2300);
//创建轨迹栏以更改HoughCircle中的最小半径
int minR=0;
createTrackbar(“minRadius”、“Trackbar窗口”和minR,300);
//创建轨迹栏以更改HoughCircle中的最大半径
int maxR=0;
createTrackbar(“maxRadius”、“Trackbar窗口”和maxR,300);
//调试目的

cout我最近使用了Houghcirle函数。我有一点经验。你声明圆(变量)是全局变量,如果你移动直升机,但Houghcirle fcn还没有找到圆。它将保留圆值(中心(x,y),半径)在过去,图像看起来像是延迟。您可以将Hough的程序作为子例程编写。vector circles变量在此子例程中是局部变量。可能会改善延迟。

也许您可以先执行一些成本较低的操作来查找感兴趣的区域,而不是在整个图像上执行Hough circles,如f在图像中找到一个亮点(就像你的白圈一样)对不起,这是不允许的。谢谢你的回答。我很难理解为什么你的教授会认为这是作业中的一个漏洞(只要你仍然在通过处理找到的ROI中做hough)。您可以设置“最小圆半径”和“最大圆半径”以查看这是否会提高性能。要知道哪些部分需要更多时间来添加计时器,例如。如果这样做,请删除couts。此外,
dparam1=param1/1.0;
应为
dparam1=(双精度)param1;
:)绘图确实会减慢速度。但是如何分析您的代码以找出瓶颈?您好,谢谢大家的回答。有没有办法限制绘制的圈数,即使检测到很多圈?my
cout
告诉我它卡在2和3之间,检测部分也会减慢速度因为圆圈太多了?