Speeding up HoughCircles

Speeding up HoughCircles

本文关键字:HoughCircles up Speeding      更新时间:2023-10-16

我对houghcircles函数有问题,关于它的处理速度。当我试图用这个视频文件运行代码时,我上传到Youtube上。https://youtu.be/5RGRTPEdHVY

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

现在我不想手动做了,有没有一种方法可以智能地做?

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

我还想知道是霍夫圈减慢了事情的速度还是画了圆?

谢谢

附完整代码。

#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencvcv.h>
#include <opencvhighgui.h>
#include <stdlib.h>
#include <stdio.h>
#include <opencv2opencv.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个圆圈。

如果你有一些方法,请评论。

我最近使用了Houghcircle函数。我有一点经验。你声明圆圈(变量)它是全局变量,如果你移动直升机但是HoughCircle fcn还没有找到圆圈。它将保持过去的圆值(中心(x,y),半径),因此图像看起来像一些延迟。你可以把霍夫的程序写成一个子程序。矢量圆变量是这个子程序中的局部变量。