我通过opencv玩avi,我试图对视频进行一些圆圈检测。只是播放视频很棒但是当我尝试检测圆圈时,视频会变慢。有没有办法让视频播放的速度接近录制的速度?
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
int main(int argc, char** argv)
{
IplImage*img;
int key=0;
CvCapture*capture = cvCaptureFromAVI("C:\\Users\\Nathan\\Desktop\\SnookVid.wmv");
if(!capture) return 1;
int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
cvNamedWindow("circles", 0);
while(key!='q'){
img = cvQueryFrame( capture );
if(!img) break;
IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
//IplImage* hsv = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
CvMemStorage* storage = cvCreateMemStorage(0);
//covert to grayscale
cvCvtColor(img, gray, CV_BGR2GRAY);
// This is done so as to prevent a lot of false circles from being detected
cvSmooth(gray, gray, CV_GAUSSIAN, 3, 5);
IplImage* canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
//IplImage* rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
//cvCanny(gray, canny, 50, 70, 3);
//detect circles
CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, 27, 70, 40,0,0);
//cvCvtColor(canny, rgbcanny, CV_GRAY2BGR);
//cvCvtColor(img,hsv, CV_BGR2HSV);
//draw all detected circles
float* p;
CvScalar s;
int num_red = 22;
for (int i = 0; i < circles->total; i++)
{
// round the floats to an int
p = (float*)cvGetSeqElem(circles, i);
cv::Point center(cvRound(p[0]), cvRound(p[1]));
int radius = cvRound(p[2]);
//uchar* ptr;
//ptr = cvPtr2D(img, center.y, center.x, NULL);
//printf("B: %d G: %d R: %d\n", ptr[0],ptr[1],ptr[2]);
s = cvGet2D(img,center.y, center.x);//colour of circle
printf("B: %f G: %f R: %f\n",s.val[0],s.val[1],s.val[2]);
}
答案 0 :(得分:2)
在使用HoughCircles
pyrDown或resize #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat circleBig = imread("circle.png", 0);
Mat circleSmall;
double scale = 2.0;
// INTER_NEAREST is crude, but very fast; you may need INTER_LINEAR here...
resize(circleBig, circleSmall, Size(0, 0), 1.0 / scale, 1.0 / scale, cv::INTER_NEAREST);
cvtColor(circleBig, circleBig, CV_GRAY2RGB);
vector<Vec3f> circles;
HoughCircles(circleSmall, circles, CV_HOUGH_GRADIENT, 2, circleSmall.rows >> 2, 200, 100 );
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][3]));
int radius = cvRound(circles[i][4]);
// draw the circle center
circle( circleBig, scale*center, 3, Scalar(0,255,0), -1, 8, 0 );
// draw the circle outline
circle( circleBig, scale*center, scale*radius, Scalar(0,0,255), 3, 8, 0 );
}
imshow("circleBig", circleBig);
waitKey();
return 0;
}
进行处理之前,尝试缩小图像尺寸。
如果要将检测到的圆圈与原始图像一起使用,请将半径和中心乘以您划分图像的系数。减少2倍的比例可以使处理时间减去2-4倍,减去执行比例操作所需的时间。
以下是您可能会如何解决这个问题的简短示例:
HoughCircles
最后,这是我使用的测试图像:
以下是640x480 time: 0.0127101
320x240 time: 0.00408843
检测的时间:
{{1}}
粗略地说,加速3倍! :)
答案 1 :(得分:0)
你可以尝试mevatron的建议,它肯定会有所作为,这就是我赞成它的原因。
但有一点需要明确的是:视频速度减慢不是源代码中的错误,它也不是OpenCV中的错误。此效果是由CPU不得不花费处理时间对视频的每一帧执行圆检测引起的。你的CPU根本无法快速执行这项任务,无法给你实时的感觉。
问题的标题有点误导,因为你不只是用OpenCV播放AVI。