Why is imread() always returning null data? - c++

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.

Related

OpenCV Video capture from file won't open successfully

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

OpenCV on eclipse on windows

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>

OpenCV VideoCapture not working outside Visual Studio

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.

How to load xml cascade file in openCV 2.3.1

I try bulid face detect applicatyion on Visual Studio 2010 using C++ and OpenCV 2.3.1 library
So, I declaration String type and initialize:
String face_cascade_name = "haarcascade_frontalface_alt.xml";
Next, I create a object of class CascadeClassifier:
CascadeClassifier face_cascade;
And I load cascade:
if( !face_cascade.load(face_cascade_name) ){ printf("--(!)Error loading\n"); return -1; };
The project bulid and debugging without problem, but when I run application and application try load cascade program crash!
And I see this communication:
The program '[1288] OpenCV2.0.exe: Native' has exited with code -1 (0xffffffff).
I had the same problem with CascadeClassifier and FileStorage.
For example if you try this:
FileStorage fs(xml_fname, FileStorage::READ);
if (!fs.isOpened())
{
cout<<"can not read xml"<<endl;
}
Probably, it won't work.
In my case, I passed in VC++ from Debugging mode to Release mode, specified .lib files without d at the end (e.g. opencv_core231.lib) and it works now.
In my case(OSX 10.9), I input the whole path instead of simply "haarcascade_frontalface_alt.xml", like "/Users/xxx/Desktop/opencv-2.4.7/data/haarcascades/haarcascade_frontalface_alt.xml". Good Luck
In my case (win7 64, VS 10 express), changing file permissions for xml files solved the problem,
I added Everyone with full controll, and it worked.

OpenCV cvLoadImage() does not load images in visual studio debugger?

I am trying to work out a simple hello world for OpenCV but am running out of ideas as to why it is not working.
When I compile and run this code:
#include <cv.h>
#include <highgui.h>
int main(int argc, char* argv[])
{
IplImage* img = cvLoadImage( "myjpeg.jpg" );
cvNamedWindow( "MyJPG", CV_WINDOW_AUTOSIZE );
cvShowImage("MyJPG", img);
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "MyJPG" );
return 0;
}
I get a grey box about 200x200 instead of the indicated .jpg file. If I use a different jpg I get the same kind of window, and if I put an invalid filename in, I get a very tiny window (expected).
I am using Visual Studio 2008 under Windows 7 Professional.
Most of the sample programs seem to work fine, so I am doubly confused how that code loads the sample jpgs just fine but in the code above it does not work (even tried the sample jpeg).
Update
The executables produced by compiling work fine, however the Visual Studio 2008 debugger loads a null pointer into img every time I try to run the debugger - regardless if the file location is implicit or explicit.
It really seems like there's a problem with the path to myjpeg.jpg since the current directory could be different when you're running under the debugger.
By default, the current directory that the Visual Studio debugger uses is the directory containing the .vcproj file, but you can change it in the project properties (Debugging -> Working Directory).
Are you 100% sure that you pass the absolute path correctly? Try to pass the same path to fopen and see if it also returns NULL. If so, then the path is incorrect.
If you want to see exactly what file is the library trying to open you can use Project Monitor with a filter on myjpeg.jpg.
Which version of OpenCV are you using? I've tried your code on the latest (OpenCV2.0) and it works fine. You can download OpenCV2.0 from here.
If you want the latest build, you can get check it out with SVN from here.
Try to add HAVE_JPEG into preprocessor definitions.
I encountered the same problem. The Debug version does not load the image, but when I compile and link it as a Release it works. I hope this helps