video playing in opencv: no error no output - c++

I have written a program to display a stored video file using opencv. I attached the code below. I am not getting any errors while building it but no output is displayed.
int main(int argc, char *argv[])
{
if (argc <= 1)
{
printf("Usage: %s video\n", argv[0]);
return -1;
}
VideoCapture capture(argv[1]);
namedWindow("display",cv::WINDOW_AUTOSIZE);
capture.set(cv::CAP_PROP_FRAME_WIDTH, 640);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
if(!capture.isOpened())
{
printf("Failed to open the video\n");
return -1;
}
int i;
for(i=0;i<390;i++)
{
Mat frame;
capture >> frame; // get a new frame from camera
cout << "frame =" << endl << " " << frame << endl << endl;
imshow("display",frame);
}
}
I included the cout line at the end to check if the frame is getting any value or not. So a got a number of values in a matrix, but the video window is not appearing.

You have to add a very small delay after imshow by using waitKey.
imshow("display",frame);
waitKey(10); //Wait 10 milliseconds before showing next frame.

Related

Raw footage not appearing while using OpenCV in XCode [duplicate]

I have written a program to display a stored video file using opencv. I attached the code below. I am not getting any errors while building it but no output is displayed.
int main(int argc, char *argv[])
{
if (argc <= 1)
{
printf("Usage: %s video\n", argv[0]);
return -1;
}
VideoCapture capture(argv[1]);
namedWindow("display",cv::WINDOW_AUTOSIZE);
capture.set(cv::CAP_PROP_FRAME_WIDTH, 640);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 480);
if(!capture.isOpened())
{
printf("Failed to open the video\n");
return -1;
}
int i;
for(i=0;i<390;i++)
{
Mat frame;
capture >> frame; // get a new frame from camera
cout << "frame =" << endl << " " << frame << endl << endl;
imshow("display",frame);
}
}
I included the cout line at the end to check if the frame is getting any value or not. So a got a number of values in a matrix, but the video window is not appearing.
You have to add a very small delay after imshow by using waitKey.
imshow("display",frame);
waitKey(10); //Wait 10 milliseconds before showing next frame.

how to record webcam video with opencv2.4.9?

#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "ERROR: Cannot open the video file" << endl;
return -1;
}
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("D:/MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
I am developing this project using Visual Studio 2012 express and opencv_2.4.9 and windows 7 x64.
But, this code is not working.
Just write out this message "ERROR: Failed to write the video".
Use try, catch to see if you can get a more detailed error message.
One thing I remember when writing videos is that you need to verify you have the codecs installed and in the correct directory or added as a PATH environment variable.
opencv_ffmpeg.dll was the issue for me, and making sure it was in the correct directory and being renamed to opencv_ffmpeg249.dll

MP3 header missing from IP camera RTSP

I just want to show the IP camera video without sound, but this camera plays video with sound, and I get [mp3 # 0x107808800] Header missing in the console. How do I play without sound? I'm using vstarcam-30s. Here is my code:
int main (int argc, char *argv[])
{
VideoCapture vcap;
Mat image;
string videoStreamAddress =
"rtsp://[IPADDRESS]/profile2/media.smp";
if (!vcap.open(videoStreamAddress)) {
cout << "Error opening video stream or file" << endl;
return -1;
}
for(;;) {
if(!vcap.read(image)) {
cout << "No frame" << endl;
waitKey();
}
imshow("Output Window", image);
if(waitKey(1) >= 0) break;
}
return 0;
}

Trying to get 1080p live feed from HD webcam using Qt and OpenCV only get 480p

I am trying to get full HD processing using Qt and OpenCV, I can only get 480p at the moment, as you can see in the code I have got the width and height of the frame. I have also tried setting the size of the using cvSize(1920 x 1080) but it doesn't change the resolution.
Many thanks for any help!
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VideoCapture cap(1); //capture webcam
if (!cap.isOpened()) //if not successful then exit
{
qDebug() << "Cannot open webcam";
return -1;
}
namedWindow("Camera feed", CV_WINDOW_AUTOSIZE); //create window
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get width of frames of video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get height of frames of video
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
qDebug() << "Frame size = " << dWidth << "x" << dHeight << endl;
VideoWriter oVideoWriter("video.avi", CV_FOURCC('M','P','E','G'), 20, frameSize);
if(!oVideoWriter.isOpened())
{
qDebug() << "ERROR: Failed to write the video" << endl;
return -1;
}
while(1)
{
Mat frame;
bool bSuccess = cap.read(frame); //read a new frame from video
if(!bSuccess) //if unsuccessful, break loop
{
qDebug() << "Cannot read frame from video file" << endl;
break;
}
oVideoWriter.write(frame); //write the frame into the file
imshow("Camera feed", frame); //show the frame in "Live Feed" window
qDebug() << "Recording" << endl;
if (waitKey(30) == 27)
{
qDebug() << "Esc key is pressed by user" << endl;
break;
}
}
return 0;
}
Have you tried setting forcing your capture to HD by setting the properties:
cap.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
If so, check if this resolution is supported by the device in other programs like guvcview or v4l2-ctl. If you have the last one installed you can check the supported modes with:
v4l2-ctl --list-formats

Video display window in OpenCV not sizing up to video

I have written the following code below to display a video in OpenCV. I have compiled it fine but when I run it, the window that is supposed to show the video opens but it is too small to actually see if the video is playing. Everything else seems to be working fine. The width, height and number of frames are printed on the command line as coded. Anyone know what the problem is? Check it out.
void info()
{
cout << "This program will accept input video with fixed lengths and produce video textures" << endl;
}
int main(int argc, char *argv[])
{
info();
if(argc != 2)
{
cout << "Please enter more parameters" << endl;
return -1;
}
const string source = argv[1];
VideoCapture input_vid(source);
if(! input_vid.isOpened())
{
cout << "Error: Could not find input video file" << source << endl;
return -1;
}
Size S = Size((int) input_vid.get(CV_CAP_PROP_FRAME_WIDTH), //Acquire size of input video
(int) input_vid.get(CV_CAP_PROP_FRAME_HEIGHT));
cout << "Width: = " << S.width << " Height: = " << S.height << " Number of frames: " << input_vid.get(CV_CAP_PROP_FRAME_COUNT)<<endl;
const char* PLAY = "Video player";
namedWindow(PLAY, CV_WINDOW_AUTOSIZE);
//imshow(PLAY,100);
char c;
c = (char)cvWaitKey(27);
//if ( c == 27)break;
return 0;
}
assuming video is from webcam:
capture = CaptureFromCAM( 0 );
SetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT, 640);
SetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH, 480);
this will fix your problem
another simple tweak could be using CV_WINDOW_NORMAL instead of CV_WINDOW_AUTOSIZE
namedWindow(PLAY, CV_WINDOW_AUTOSIZE);
which lets you resize the window manually