OpenCV show video file

asked 2018-01-16 01:55:06 -0600

zobelx gravatar image

Hi, my IDE is QT Creator 5.7.0, compiling with MSVC (2015). I use the OpenCV 3.4.0 My OS is Windows 7 (64 Bit)

When i try to capture and "imshow" the .avi -Video, there is popping up only a gray Window and when the Video is ending, it Shows the last Picture of the Video.

Is it a Problem with opencv_ffmpeg340_64.dll? I still tried to set the Path-Variables, to copy the .dll into the debug Folder, etc.

// Auswahl Video
cv::VideoCapture cap("Regen_02_400fps_K3Stellung_170Belichtung.avi");

// Meldung wenn Video nicht gefunden wird
if (!cap.isOpened())
    std::cout<<"Error when reading steam";
//cv::namedWindow("Original");

for(;;)
{
    cap.read(frame);

    // Abbruch wenn kein Frame mehr gefunden wird
    if (!cap.read(frame))
        break;

    cv::imshow("Original", frame);
    cv::waitKey(25);

    // Abfolge "Geschwindigkeit" mit WaitKey
    char key = cvWaitKey(50);

    // ESC drücken um zu unterbrechen
    if (key == 27)
        break;

 }
edit retag flag offensive close merge delete

Comments

You repeat each operation twice. You repeat each operation twice ;)

You read the frame twice (so you'll lose every second frame) and capture the keypress twice (so it might not be read.

Try this:

int num=0;
while(cap.read(frame)) {
    cout<<"Frame "<<num++<<endl;
    imshow("Original",frame);
    key=waitKey(30);
    if(key==27) break;
}
cout<<"End of video<<endl;
kbarni gravatar imagekbarni ( 2018-01-16 03:52:55 -0600 )edit

First of all, thanks for your reply. But it hasn´t solved my Problem :(

I changed to...

int num=0;

while(cap.read(frame))
{
    std::cout << "Frame "<< num++ << std::endl;
    cv::imshow("Original",frame);
    char key = cv::waitKey(30);
    if(key==27) break;
}
std::cout << "End of video" << std::endl;

It still open a Window which is filled gray, only the last Frame of the Video is shown. These are the last lines of the application Output:

Frame 6979 Frame 6980 Frame 6981 Frame 6982 Frame 6983 End of video Unexpected list type. Expected: Isn´t it a Problem with the opencv_ffmpeg340_64.dll, that it probably can´t decode my Video?

zobelx gravatar imagezobelx ( 2018-01-16 04:50:15 -0600 )edit

Try to read only one frame (without the while loop) or read the frames one by one after keypress (key=cv::waitKey()). Is it still the same gray image? Try debug by displaying the value of a given pixel (std::cout<<frame.at<Vec3b>(10,10)<<std::endl;) Is it constant?

If the images are correctly read, I suspect some kind of threading issue (OpenCV cannot display the images for some reason while decoding the video...)

kbarni gravatar imagekbarni ( 2018-01-16 09:44:11 -0600 )edit

Again thanks for reply. I tried applied both your advices, unfortunatelly without success.

while(cap.read(frame))
{
    std::cout << "Frame "<< num++ << std::endl;
    cv::imshow("Original",frame);
    std::cout << (int)frame.at<uchar>(10,10)<<std::endl;
    char key = cv::waitKey();

// if(key==27) break; } std::cout << "End of video" << std::endl;

These are the last lines auf application Output: Frame 6981 230 Frame 6982 229 Frame 6983 230 End of Video

The Pixel values are realistic, the Video is at (10,10) nearly complete White (grayscale Video). There must be any Problem to Display the Video. If i open a single picture, it works to display that. But I am not abe to zoom into the Window, to see the single Pixel values. What could be the Problem?

zobelx gravatar imagezobelx ( 2018-01-17 02:20:05 -0600 )edit

So, a Little update: I deleted the opencv 3.4.0 and switched to 2.4.13. Now i´´m able to Display my Video with the following code:

int num=0;

while(cap.read(frame))
{
    std::cout << "Frame "<< num++ << std::endl;
    cv::imshow("Original",frame);
    std::cout << (int)frame.at<uchar>(10,10)<<std::endl;
    char key = cv::waitKey(1);
    if(key==27) break;
}
std::cout << "End of video" << std::endl;

The only Thing is, that i´m not able to zoom into the window, to see the Pixel values. Whats wrong there?

zobelx gravatar imagezobelx ( 2018-01-17 06:30:34 -0600 )edit

Can you get the window toolbar if you create the window using:

cv::namedWindow("Original",cv::WINDOW_NORMAL|cv::WINDOW_KEEPRATIO);

(the syntax might differ for OpenCV 2.4). If it doesn't display the toolbar (with the zoom buttons) you ight need to rebuild it using the WITH_QT flag.

kbarni gravatar imagekbarni ( 2018-01-17 06:50:19 -0600 )edit

It doesn´t work. What do you mean by rebuildung with WITH_QT?

zobelx gravatar imagezobelx ( 2018-01-17 07:40:17 -0600 )edit

Yes. These functions require QT support, and OpenCV needs to be rebuilt if it doesn't have this. See the highgui documentation for QT functions

kbarni gravatar imagekbarni ( 2018-01-17 11:16:23 -0600 )edit