Problem in opening video file using captureVideo
Hi All, I'm kind of new to opencv and would very much appreciate your help on solving the following problem: (I have Visual studio C++ 2010 and OpenCV 2.4.4 installed.)
I am trying to open a video from a file using captureVideo(filename). When I do this I can read the correct frame rate and a resolution of my video file (also the type of a codec). When I use imshow to show the first frame the resolution of my window is correct, but the image is all grey. When I enter the while(1) loop and try to get a new frame, the read resolution is [something x 0], the frame rate is 0 and the codec is not given. I have tried this for both release and debug modes (32bits) with exactly the same effect. However, when I use a very similar code to capture video from a webcam everything works fine.
int TipTracking(char directive[], char filename[]);
using namespace std; using namespace cv;
int main(int argc, const char* argv[]) { TipTracking("-v","filename"); }
int TipTracking(char directive[], char filename[]) {
cv::VideoCapture captureVideo; // declares video device _FILE
cv::Mat captureFrame, MatEdges; // declares frame matrices
if(directive == "-v" )
{
VideoCapture captureVideo( // open the video file for reading
filename
);
if (!captureVideo.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl; // command line text
return -1;
}
fps = captureVideo.get(
CV_CAP_PROP_FPS // get the frames per seconds of the video
);
bSuccess = captureVideo.read( // read a new frame from video
captureFrame
);
if (false == bSuccess) // if not success, exit the program
{
cout << "Cannot read from the video file" << endl;
return -1;
}
FrameSize = Size( // declare FrameSize as a size of a frame
captureFrame.cols,
captureFrame.rows
);
codecType = captureVideo.get( // Get Codec Type- Int form
CV_CAP_PROP_FOURCC
);
char codecType_char [] = { // Transform from int to char via Bitwise operators
(char)(codecType & 0XFF) ,
(char)((codecType & 0XFF00) >> 8),
(char)((codecType & 0XFF0000) >> 16),
(char)((codecType & 0XFF000000) >> 24),
0
};
cout << "\n" << endl;
cout << "Video source: FILE" << endl;
cout << "Frame per seconds : " << fps << endl;
cout << "Frame size (rows x cols): " << FrameSize << endl;
cout << "Codec type: " << codecType_char << endl;
imshow( // show the image (the image window is in correct resolution but all gray only)
"outputCapture",
captureFrame
);
}
cout << "Press ENTER to continue..." << endl;
std::cin.get();
while(1)
{
bSuccess = captureVideo.read( // read a new frame from video
captureFrame
);
fps = captureVideo.get(
CV_CAP_PROP_FPS //get the frames per seconds of the video
);
FrameSize = Size( // declare FrameSize as a size of a frame
captureFrame.cols,
captureFrame.rows
);
codecType = captureVideo.get( // Get Codec Type- Int form
CV_CAP_PROP_FOURCC
);
char codecType_char [] = { // Transform from int to char via Bitwise operators
(char)(codecType & 0XFF) ,
(char)((codecType & 0XFF00) >> 8),
(char)((codecType & 0XFF0000) >> 16),
(char)((codecType & 0XFF000000) >> 24),
0
};
cout << string(1, '\n');
cout << "\n" << endl;
cout << "Video source: FILE" << endl;
cout << "Frame per seconds : " << fps << endl;
cout << "Frame size (rows x cols): " << FrameSize << endl;
cout << "Codec type: " << codecType_char << endl;
imshow( // show the image
"outputCapture",
captureFrame
);
waitKey(100); //pause for 100ms
}
return 0;
}
your 1st loop is simply lacking the waitKey(20);, thus the gray image.