问题
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
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?
Thank you
Full Code is attached.
#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", ¶m1, 300);
//Create trackbar to change param2 in HoughCircle
int param2 = 300;
createTrackbar("param2", "Trackbar Window", ¶m2, 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;
}
EDIT: 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.
回答1:
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.
来源:https://stackoverflow.com/questions/33120603/speeding-up-houghcircles