opencv - 延迟使用OpenCv播放AVI

我通过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

大概是3倍加速! :)

https://stackoverflow.com/questions/8808024/

相关文章:

opencv - OpenCV || matchShapes findContours断言失败

visual-studio-2008 - 应用程序无法启动:VS中的OpenCV x64编译使用x8

docker - 如何使用非 root 用户权限构建 docker 以使用 pipenv 设置 py

json - 在 Docker 中将 JSON 文件作为环境变量传递

opencv - 用opencv实现消失点算法

opencv - mac上opencv2.3.1中 undefined symbol ,cvSmoo

python - 用Python捕捉 Action

opencv - OpenCV中两个矩阵的广义特征值

image - 颜色键在图像处理中的作用是什么?

image-processing - cvCalculateOpticalFlowFarneback