我通过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]);
}
最佳答案
在使用pyrDown或resize用HoughCircles
处理图像之前,请尝试减小图像尺寸。
如果要将检测到的圆与原始图像一起使用,则将半径和中心乘以除以图像的因子。减少2倍的缩放比例应使处理时间减2-4倍的加速时间减去执行缩放操作所需的时间。
下面是一个简短的示例,说明了如何进行此操作:
#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;
}
HoughCircles
的时间:640x480 time: 0.0127101
320x240 time: 0.00408843
https://stackoverflow.com/questions/8808024/