Trouble reading image OpenCV - c++

I'm following the tutorial here for setting up OpenCV with Visual Studio (I have 2013 Community edition and OpenCV 2.4.10).
I have the following folder structure:
OpenCVTest
-OpenCVTest.sln
+x64
+Debug
- opencv_core2410d.dll
- opencv_highgui2410d.dll
- OpenCVTest.exe
- OpenCVTest.ilk
- OpenCVTest.pdb
- feck.png
And my source:
#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(argv[1], IMREAD_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;
}
When I run this in VS or on the command line, I get the following error:
C:\Users\mr\Documents\Visual Studio 2013\Projects\OpenCVTest\x64\Debug>OpenCVTest.exe feck.png
Could not open or find the image
Anybody know why this might be happening?
Update
I have tried giving the full path:
image = imread("C:\Users\mr\Documents\Visual Studio 2013\Projects\OpenCVTest\feck.png", IMREAD_COLOR); // Read the file
and placing the image at the same level as the .sln file, still no luck.

Place your image in OpenCVTest folder like this:
C:\Users\mr\Documents\Visual Studio 2013\Projects\OpenCVTest\OpenCVTest\feck.png
-OpenCVTest.sln
-OpenCVTest <- **HERE**
-ipch
+x64
+Debug
- opencv_core2410d.dll
- opencv_highgui2410d.dll
- OpenCVTest.exe
- OpenCVTest.ilk
- OpenCVTest.pdb
- feck.png

In the tutorial here, under "The local method", it reads:
Then you need to specify the libraries in which the linker should look
into. To do this go to the Linker ‣ Input and under the “Additional
Dependencies” entry add the name of all modules which you want to use:
opencv_core231d.lib
opencv_imgproc231d.lib
opencv_highgui231d.lib
opencv_ml231d.lib
opencv_video231d.lib
opencv_features2d231d.lib
opencv_calib3d231d.lib
opencv_objdetect231d.lib
opencv_contrib231d.lib
opencv_legacy231d.lib
opencv_flann231d.lib
I changed these from opencv_core{version}d.lib to opencv_core{version}.lib (not the debug library) and it seems to work okay now. I can step through the code in Visual Studio and the code appears to work.

Related

Suddenly cannot create new openCV applications, windows 10

I have been using openCv for a while, but have just moved to windows 10.
Now, existing applications will compile, but I cannot make a new one.
In a new project (visual studio 2015, release 64)
I am adding all the libs:
opencv_calib3d310.lib
opencv_core310.lib
opencv_features2d310.lib
opencv_flann310.lib
opencv_highgui310.lib
opencv_imgcodecs310.lib
opencv_imgproc310.lib
opencv_ml310.lib
opencv_objdetect310.lib
opencv_photo310.lib
opencv_shape310.lib
opencv_stitching310.lib
opencv_superres310.lib
opencv_ts310.lib
opencv_video310.lib
opencv_videoio310.lib
opencv_videostab310.lib
setting:
D:\opencv-master\build64\lib\Release;%(AdditionalLibraryDirectories)
and
D:\opencv-master\modules\highgui\include
D:\opencv-master\modules\imgcodecs\include
D:\opencv-master\modules\core\include
D:\opencv-master\modules\videoio\include
D:\opencv-master\modules\imgproc\include
%(AdditionalIncludeDirectories)
and adding the most basic:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include "stdafx.h"
#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(argv[1], IMREAD_COLOR); // Read the file
if (image.empty()) // 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;
}
In visual studio, Intellisense is fine, no red underlines, everything looks good. When I try to compile tho...
'cv': a namespace with this name does not exist
'Mat': undeclared identifier
'image': undeclared identifier
...and many more. It is like it cannot find the libs, but i am linking them correctly, I am sure of it.
Can anyone assist me here?

imread couldn't find image

I use visual studio 2013 and opencv. I run a simple code that reads a image and shows it. I add the image to resource files of my project from Donloads file. When I run below code, image.data is empty.
#include "stdafx.h"
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
cv::Mat image;
image = cv::imread("im.png", CV_LOAD_IMAGE_COLOR); // Read the file
if (!image.data) // Check for invalid input
{
std::cout << "Could not open or find the image" << std::endl;
return -1;
}
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);// Create a window for display.
imshow("Display window", image); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window
return 0;
}
Try : C:\\Users\\pr\\Downloads\\im.png that should work if the path is correct.
This is because the default working directory in VS is the location of your vcxproj file (value of $(ProjectDir)).
If you would like to start your application through VS (i.e. by F5 key) then you should overwrite the debugging parameters of your project. For example you can set the application's working directory to the location of the program (exe) that the linker creates by setting the value of Project menu -> Properties -> Configuration properties -> Debugging -> Working directory to $(OutDir).

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.

First steps with opencv and visual studio

i'm starting using opencv with visual studio, these are my sw components and environment:
windows 8.1 (64bit)
visual studio professional 2013
opencv 2.4.9
first of all i've downloaded opencv and extracted all in "C:\OpenCV-2.4.9", then i've created a new environment variable (user variable) OPENCV_DIR with value "C:\OpenCV-2.4.9\opencv\build" and added the string ";%OPENCV_DIR%\x86\vc12\bin" to the existing environment variable Path.
first question: do i should set x64 instead of x86 in the above string value? in general, are all settings right?
then i've created a new win32 console project with visual studio, it creates a function _tmain(int argc, _TCHAR* argv[])inside a .cpp file named .cpp.
second question: what is the reason because i've to use such '_tmain' function instead the classic 'main' function? which is the structure of the calls when i 'run' the project? is that useful? and if i wish use the classic 'main'?
then, i've defined the following code
int main(int argc, char* argv[])
{
if (argc != 2)
{
cout << " Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if (image.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl;
return -2;
}
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;
}
when i compile and debug with visual studio everything is ok! but i cannot be able to give the requested parameter to the function... it starts and ends with output code -1 (the first if check).
moreover, if i find the .exe and try to run it using the prompt, an error occurs:
"Impossible to run the program because opencv_core249d.dll is not present...... try to reinstall the program".
it is clear that these are common problems but i'm finding these issue so hard... thanks to all!

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.