I've written this in C++ (VS2012) using the OpenCV library (2.4.6).
#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
int main(){
Mat image;
VideoCapture cap;
cap.open("test.avi");
if(!cap.isOpened()){
cout<< "Capture not open \n";
cin.get();
}
cvNamedWindow("Video Output");
while(1){
cap >> image;
imshow("Video Output",image);
waitKey(30);
}
}
Running it, the video capture fails to open.
test.avi is located in the same directory as the executable, and running it ind Debug/Release/outside the IDE makes no difference.
The OpenCv DLLs and the video file are here: https://www.dropbox.com/sh/16c04d97iw90gtk/88fQ4BLbfl#/
What could I be doing wrong?
EDIT: As seen in questions on the OpenCV Q&A site, I've copied the opencv_ffmpeg DLL to the folder with my executable. Now it only works outside the IDE (VS2012)
You should copy the opencv_ffmpeg DLL in your targetDir, in this case the Debug executable folder, other than the Release one
Related
As the title says. I've tried to load an image with argv and with absolute path and it worked, but with relative path it doesn't. The image is in the same directory of the executable. I'm using visual studio 2013 and opencv 2.4.10 on windows 7 64 bit. How can I resolve?
EDIT
Here's my code:
include <iostream>
#include <fstream>
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\stitching\stitcher.hpp"
#include "opencv2\core\core.hpp"
using namespace cv;
using namespace std;
void main()
{
vector< Mat > vImg;
Mat rImg;
vImg.push_back(imread("1.png"));
vImg.push_back(imread("2.png"));
vImg.push_back(imread("3.png"));
Stitcher stitcher = Stitcher::createDefault(true);
unsigned long AAtime = 0, BBtime = 0; //check processing time
AAtime = getTickCount(); //check processing time
stitcher.stitch(vImg, rImg);
BBtime = getTickCount(); //check processing time
printf("%.2lf sec \n", (BBtime - AAtime)
getTickFrequency()); //check processing time
namedWindow("Stitching Result");
imshow("Stitching Result", rImg);
waitKey(0);
}
I tried also to use "./" "/" "//" "\" and "\",but it still not working!
As CTZStef said, the images must not be in the sln folder but in the folder with the VC++ project file!
I could offer you the following way to deal with your problem:
In your IDE change the working directory in which your application is executed when run or debug it. Change it to the root of your data directory.
Start a command prompt and cd to the root of your data directory. Then start your application using the absolute path to it.
Add a compile definition which stores the absolute path of your data directory (add something like this /DDATA_DIR=C:\where\your\data\is to your compiler flags and in your code use vImg.push_back(imread("DATA_DIR/1.png"));)
Generally, if possible, I would keep your source code separate from your data and not copy it into the folder of your solution file.
Here is my code:
#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace cv;
using namespace std;
int main()
{
Mat src;
//src.create(200,500,CV_8UC3);
src = imread( "a.bmp", 1 );
namedWindow( "Display window", WINDOW_AUTOSIZE );
if(!src.data)
cout<<"Could not open or find the image" << std::endl ;
else
imshow( "Display window", src);
waitKey(0);
return 0;
}
It is always executing the if part
when I am using src.create instead of imread() it shows an empty image.
To debug your issue you should try to confirm that the image path is correct.
As suggested in the comments, try specifying the full absolute path of the file. Remember to to use escape slashes if you are on windows (e.g. c:\a.bmp will need to be "c:\a.bmp")
OR
If you are executing your application from Visual Studio then you can configure the working directory to be that of the bitmap too! (OpenCV cvLoadImage() does not load images in visual studio debugger?)
You can also try using cvLoadImage instead of imread. If cvLoadImage can open the file then it is possible that you have a mix of release and debug libraries causing you an issue as per:
OpenCV imread(filename) fails in debug mode when using release libraries
The OpenCV documentation has mentioned imread() would return an empty matrix ( Mat::data==NULL ) if the image file cannot be read.
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imread
You should check if the "a.bmp" file is in your current working directory. The IDE (visual studio) may set executable's working directory (Debug/... Release/... ) on purpose. Using an absolute path to "a.bmp", or starting executable in an expected directory from command line would also help, provided that "a.bmp" is a valid BMP file and you have the right file system permission to read it.
I was having the same issue on visual studion and imread returning null data.
img = cv::imread("c:\\data\\a.bmp",1);
Note the \\ delimiters to enable windows to correctly parse the path.
Your opencv libs should match the configi.e., Debug or Release of your application in Visual Studio or whatever build you are using (If you are using pre-built binaries) for WINDOWS.
when i tried to work with a video file i can't seem to open the file
when i test on isOpen() it gives me thats it did not open
what did i already check:
videofile working and in correct path
rebooting
reinstalling ffmpeg (with diferent configurations)
my code:
VideoCapture readVideo;
readVideo.open(argv[1]);
Mat frame;
if(!readVideo.isOpened()){
fprintf(stderr,"video niet geladen 0 \n");
return 2;
}
Example of the file i give in argv[1]:
out.avi (mpeg4 codex)
it works on someone elses setup so i know its not the code
The problem seemed to be with the installation of the ffmpeg that wasn't compleetly linked to the instalation of opencv, installed bouth again and worked like a charm
I'm trying to install opencv on windows, here are my steps:
downloaded opencv 2.4.3 from website
run the exe, extracted the folder in the same path
opened eclipse (with MinGW previously set and configured)
created new project XYZ
added new folder "src"
added new class "main.cpp"
added the following code:
hash include <cv.h>
hash include <highgui.h>
using namespace cv;
int main(int argc, char** argv) {
Mat image;
image = imread(argv[1], 1);
if (argc != 2 || !image.data) {
printf("No image data \n");
return -1;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
added the two paths
"E:\Sources\opencv\build\include"
"E:\Sources\opencv\build\include\opencv"
got the compilation error "Symbol 'cv' could not be resolved"
Please advice if any step is missing
you gonna need the latest stable version of openCV 2.4.3 .
Eclipse Juno ! (Eclipse IDE for C/C++ Developers )
And
MinGW - Minimalist GNU for Windows
we will ignore x86/64 choice, since we gonna work with a 32 compiler / and 32 openCV build, even though the system is a 64 one !
Step 1 : Download and Install
Eclipse
Download Eclipse from and decompress the archive . ( I assumed that you already have JRE on your computer , if not ! download and install it ) .
MinGW
Download MinGW . the installer will lead you through the process !
you might have to add the bin directory to the path ! (Default path : C/MinGW/bin )
OpenCV
Download openCV exe from the link , extract the files (in the C:/ directory in this tutorial ) .
Make sure you have the file structure below .
don't forget to add the bin directory => Path !
As I mentioned earlier ! I'll use x86 build even if i have a 64 OS to avoid compiler problems and to keep this tutorial open to x86 OS users !
Step 2 : Create and Configure
Open the Eclipse IDE !
Create a new C++ project : File > New > C++ Project
Choose a Hello Word Project to have a pre structured one !
Don't forget to select the MinGW toolchains
Click Finish and let's get to work !
Now that you have you first Hello word project ! replace the Code in the Soure file .cpp by the code below
///////////////CODE///////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lenna.png", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
///////////////CODE///////////
Obviously there are multiple errors on the code , yes ! we have to link the libraries !
Now go to Properties >> C/C++ Build >> Settings
on the Tool Setting tab >> GCC C++ Compiler >> Includes and include opencv path !
[opencvDir\build\include]
Now scroll to MinGW C++ Linker >> Libraries and add the Library search path [opencvDIR\build\x86\mingw\lib]
in the Libraries part ! we add as much librarie as we need for the project !
here I added 4 libraries just for tutorial sake even if well need only the highgui one for our test code to work !
The libraries names can be found on the [opencvDIR\build\x86\mingw\lib]
Example ! for libopencv_video243.dll.a wee add opencv_video243 in the linker !
click OK !
Now we can build our first project !
You figured that you have to add a picture to the project as implied in the source code "lenna.png"
Use lenna for good luck
Build and Run the project !
If you see the beautiful lady :) Congrats :)
have a look here for snapshots!
opencveclipse-on-windows
cv.h is for the old C API. To use the Cpp API try the following:
#include <opencv2/opencv.hpp>
I'm using VideoCapture capture(filename); to load a video from a file. When I run the program in visual studio (release mode), it works just fine, loading the video like I would expect. When I run outside of visual studio (by double-clicking the icon in the explorer directory) the video cannot be found and the capture device returns null, even though it's the same file and paths are hardcoded and absolute.
Any ideas?
Update: Also tried using the old CvCapture* and same error.
Update 6/19:
Here's some example code.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** args)
{
const char* filename = "c:/testvideo.wmv";
//Check to see if we can see the file
FILE* myFile = fopen(filename, "r");
if (myFile)
cout<<"0: Found file"<<endl;
else
cout<<"0: File not found"<<endl;
//First use the openCV new way of doing it
VideoCapture capture1(filename);
if (capture1.isOpened())
cout<<"1: Opened the video successfully"<<endl;
else
cout<<"1: Could not open the video"<<endl;
//Second, try the old way
CvCapture* capture2 = cvCaptureFromFile(filename);
if (capture2)
cout<<"2: Opened the video successfully"<<endl;
else
cout<<"2: Could not open the video"<<endl;
//Pause
char c;
cin>>c;
return 0;
}
In Visual Studio running in release mode I get:
0: File Found
1: Opened the video successfully
2: Opened the video successfully
Running from the exe from the file system (double-clicking) I get:
0: File Found
1: Could not open the video
2: Could not open the video
I only compiled once, so there's only one exe in the directory...
I've also tried displaying the frames in Visual Studio, so I know it's actually really reading the video when it thinks it's open.
Check if all the DLL's that are required are in the same folder as your exe (or in PATH)
Make sure that you use absolute video path (if not, Try to copy video into exe path) and if you are using release mode, all dlls must be in the release mode. Maybe I will solve this problem if you send me a small project.