OpenCV VideoCapture can't open file, what am I doing wrong? - c++

I am compiling on Ubuntu 14.04 using OpenCV 3.1. When trying to open a video file it gives this error:
"Cannot open the video file"
I installed everything i could install : ffmpeg etc. Haven't found a solution checking out similar questions on StackOF.
What do ?
cv::VideoCapture cap(argv[1]);
Where argv[1] is the file name in the same directory as the executable.

In case your constructor is failing, you may want to use the .open() method. So, if you want to open a file that is called "myVideo.mp4" that is in the folder of your project, you would do the following:
cv::VideoCapture cap;
cap.open("myVideo.mp4");
For more detailed informations about this method, check this documentation link
Also, the book Learning OpenCV 3, from the O'Rilley media, on page 26 gives you a good example. Here is a Gist that I made to give you as an example.
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
int main() {
cv::VideoCapture cap;
cap.open("myVideo.mp4" );
cv::namedWindow( "myVideo", cv::WINDOW_AUTOSIZE );
cv::Mat frame;
while(true) {
cap >> frame;
if( frame.empty() ){
std::cout << "Could not load the video frames. \n";
break;
}
cv::imshow( "myVideo", frame );
if( cv::waitKey(27) >= 0 ){
std::cout << "Escape pressed \n";
break;
}
}
return 0;
}

Related

I build opencv 3.4 with Gstreamer MSVC 1.16.1, and now imread and VideoCapture not working

I build OpenCV 3.4 with Gstreamer MSVC 1.16.1, using Cmake and Visual Studio 10.
I have include bin directory to the system path variable, added all additional include and library to Visual Studio.
Now when I am trying to read an Image to test if OpenCV is correctly installed it throws an error as:
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file .../opencv/modules/highgui/src/window.cpp
The code was:
Mat image1;
image1 = imread("‪D:\\Capture2.JPG");
if(! image1.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
}
imshow("Image",image1);
cvWaitKey(0);
return 0;
Now I tried to play a video using demo code from openCV site:
// opencv_3.4_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
// Create a VideoCapture object and open the input file
// If the input is the web camera, pass 0 instead of the video file name
VideoCapture cap("‪Wildlife.mp4");
// Check if camera opened successfully
if(!cap.isOpened()){
cout << "Error opening video stream or file" << endl;
return -1;
}
while(1){
Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
// Display the resulting frame
imshow( "Frame", frame );
// Press ESC on keyboard to exit
char c=(char)waitKey(25);
if(c==27)
break;
}
// When everything done, release the video capture object
cap.release();
// Closes all the frames
destroyAllWindows();
return 0;
}
The program is building correctly but I am getting following error while running it:
warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:808)
warning: ?Wildlife.mp4 (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:809)
GStreamer: error opening bin syntax error
Where can be the error as they both are simplest OpenCV program.
So the error was, there was an invisible character ('\u202A') present just after " of the filename. Once I deleted it, everything runs fine.
I found this from the warning C4566: character represented by universal-character-name '\u202A' cannot be represented in the current code page (1252)

VideoCapture not working C++ windows

So I have played around in OpenCV a bunch before and never run into this problem. I am implementing a MeanShift algorithm and trying to do it on video devices, images, and videos. Devices and images work; however, no matter what I try, when I run VideoCapture on my filename (whether setting it in the Constructor or using the VideoCapture::open() method, and whether local or with a full path) I always get stuck in my error check.
Thoughts? Ideas? code below. running in Visual Studio 2012
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\opencv.hpp"
#include "opencv2\video\video.hpp"
#include <string>
using cv::Mat;
using std::string;
enum Filetype{Image, Video};
int main(int argc, char* argv[])
{
string filename = "short_front.avi";// "C:\\Users\\Jonathan\\Videos\\short_front.mp4"; //"hallways.jpg";
Mat cv_image; //convert to unsigned char * with data
Mat filtImage_;
Mat segmImage_;
Mat whiteImage_;
cv::VideoCapture vid;
vid.open("C:/Users/Jonathan/Desktop/TestMeanShift/TestMeanShift/short_front.avi");
cv::waitKey(1000);
if ( !vid.isOpened() ){
throw "Error when reading vid";
cv::waitKey(0);
return -1;
}
// cv_image = cv::imread(filename);//, CV_LOAD_IMAGE_COLOR);
// if(! cv_image.data){
// std::cerr << "Image Failure: " << std::endl;
// system("pause");
// return -1;
// }
//Mat cv_image_gray;
//cv::cvtColor(cv_image,cv_image_gray,CV_RGB2GRAY);
for (;;)
{
vid >> cv_image;
if ( !cv_image.data)
continue;
cv::imshow("Input",cv_image); //add a normal window here to resizable
}
EDIT: This is a distinct problem from the one listed here because it deals with a specific corner case: VideoCapture and ImageCapture both work, only not VideoCapture with a file. When it doesn't work, the code runs properly, except that the "video" it creates is incomplete as it didn't open properly. Therefore, as the code above does not crash in compile time or run time, the only indicator is bad output (6KB video output file). If you are having issues not with the corner case I am describing but general issues with the above functions in OpenCV, the aforementioned link could help you.

imread in OpenCV

I found that this question has been asked many times here, but I haven't found any solution or work-around to this problem. Here's my code (copied from here: http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html):
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int _tmain(int argc, char** argv)
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
I compiled this using Visual Studio 2008 and 2010, and got different results (both don't work). The program compiled using VS 2008 has run-time error at imread(), and the other displays the message "Could not open or find the image".
Anybody can help me with this?
The problem here is your main() function._tmain does not exist in C++. main does.
_tmain is a Microsoft extension. Here is a nice explanation of these two methods.
Further more if you want to add default argument in Visual studio please follow these steps.
Right click your project in Solution Explorer and select Properties from the menu
Go to Configuration Properties -> Debugging
Set the Command Arguments in the property list.
Hope this solves your problem!
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
if( argc != 2)
{
cout <<"No Commandline Aurgument Found!: Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
set argv[1] to be a known image page "C:\test.jpg"
Ok, Ive read all the comments and I'm going to answer the main question along with the sub questions.
Why Doesn't my Code Work in VS2008?
The reason your code doesn't work in VS2008 is because you are using the compiled libraries for 2010, at least I think this is a pretty accurate assumption. If you want to be completely accurate then build the libraries, for the compiler you are using.
What is tmain & what is main
This stack overflow question answers the subject a lot better than I ever could, but effectively it is a windows specific main and does not actually exist in C++. It get removes by the compiler on compile time and converter to main.
Can you please try this: Use known-to-work image and absolute path until it works so you can be sure that there is no problem with image or relative path.
download http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png to C:\Lenna.png
rename your main function to something else and try this: if it does not work, please tell me the displayed name of the output window.
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
Mat image;
image = imread("C:/Lenna.png", CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.cols ) // Check for invalid input
{
cout << "Could not open or find the image. Press any key to exit." << std::endl ;
cv::waitKey(0)
return -1;
}
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Please try this and if it does not work, please tell me the displayed name of the output window.

OpenCV on OSX Mavericks with Xcode

I'm trying to run OpenCV on OSX in Xcode. I have downloaded the code from github. And used cmake to compile it.
Next I created a new Xcode project with the following code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread("img.jpg"); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Next I've set the header search path to: /usr/local/include
After that I've added the libraries from /usr/local/lib in the "Build Phases" as seen in the screenshot below.
(source: opencv.org)
However, when I try to run I get the following error:
ld: library not found for -lopencv_core.3.0.0
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Did I mis something?
In search for header path,
double tap the property. if you see the empty string
don't enter there,
slowly tap twice as it pop up a new list of properties
add the new one under the values press enter
still if its not working ,
check your string
You should add /usr/local/lib to the library search paths as well.

how to access a webcam that was symlinked by udev in opencv

I am developing an application that requires multiple webcams. In order to make sure that the correct webcam is used for each part of the application, I created some udev rules that SYMLINK the webcam to a specific name, depending on the serial number.
This works great, and I can access the camera by that name using VLC and a variety of other applications.
But when I try to access the camera by that name (or the non-syminked name given by linux) using OpenCV and python, I can't read a frame from the camera and my program hangs. The camera is opened successfully. I've created a sample application in C++ to test if it was perhaps a python/opencv related bug, but the same thing happens in C++ too.
Here is my C++ test application that doesn't work:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main (int argc, const char * argv[])
{
VideoCapture cap("/dev/my_custom_name");
if (!cap.isOpened())
return -1;
cout << "Opened..." << endl;
Mat img;
namedWindow("video capture", CV_WINDOW_AUTOSIZE);
while (true)
{
cout << "Trying..." << endl;
cap >> img;
cout << "Got" << endl;
imshow("video capture", img);
if (waitKey(10) >= 0)
break;
}
return 0;
}
I get the Opened... and Trying... messages, but not the Got message.
Any ideas on how to resolve this issue?
(This is all on linux btw).
Thanks
I figured this out. When I opened the capture in VLC, I noticed that it preixed the filename with v4l2://. When I did the same in my application, it worked!
So to reference above, "/dev/my_custom_name" should become "v4l2:///dev/my_custom_name".