Opencv:检测霍夫线时无法加载新帧

时间:2014-07-14 20:06:16

标签: c++ opencv visual-studio-2012

我试图检测由cam捕获的视频中的Hough Lines。问题是,要加载一个新帧,我必须关闭当前窗口,然后,自动打开一个带有新帧的新窗口。我只想摆脱关闭窗口加载新帧。如何在不关闭视频的情况下在单个窗口中播放视频?

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
VideoCapture stream(0);

if(!stream.isOpened()){
    cout << "\nCannot open video camera";
} else {    

    //CAPTURING FRAMES FROM CAMERA
    while( true ){
        Mat src;
        stream.read(src);

        //Mat src = imread("D:/LineDetection.png", 0);

        Mat dst, cdst;
        Canny(src, dst, 50, 200, 3); 
        cvtColor(dst, cdst, CV_GRAY2BGR); 

        vector<Vec2f> lines;
        // detect lines
        HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 );

        // draw lines
        for( size_t i = 0; i < lines.size(); i++ )
        {
            float rho = lines[i][0], theta = lines[i][1];
            Point pt1, pt2;
            double a = cos(theta), b = sin(theta);
            double x0 = a*rho, y0 = b*rho;
            pt1.x = cvRound(x0 + 1000*(-b));
            pt1.y = cvRound(y0 + 1000*(a));
            pt2.x = cvRound(x0 - 1000*(-b));
            pt2.y = cvRound(y0 - 1000*(a));
            line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
        }

        imshow("source", src);
        imshow("detected lines", cdst);

        if( waitKey() == 32 )
            break;

        }

}

return 0;
}

1 个答案:

答案 0 :(得分:5)

将waitKey()更改为waitKey(10)。

waitKey() - 将等待按键无限时间。