To be more exact, I can open it when I start in working directory via command line. If I am debugging using Visual Studio project, then the path or file is not found:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <fstream> // to check file if exists
#include <iostream> // console line output and input
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
string imageName("lena.jpg"); // by default
if( argc > 1)
{
imageName = argv[1];
}
Mat image;
ifstream f(imageName.c_str());
bool i = f.good();
std::cout << "File exists? \r\n" << i ;
image = cv::imread(argv[1], IMREAD_COLOR); // Read the file
if( image.empty() ) // Check for invalid input
{
std::cout << "Could not open or find the image" << std::endl ;
return -1;
}
cv::namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
/*
WINDOW_NORMAL - user can resize window (no constraint) / also use to switch a fullscreen window to a normal size
WINDOW_AUTOSIZE - user cannot resize window, the size is constrainted by image displayed
WINDOW_OPENGL - window with opengl support
WINDOW_FULLSCREEN - change the window to fullscreen
WINDOW_FREERATIO - image expends as much as it can (no ratio constraint).
WINDOW_KEEPRATIO - ratio of the image is respected
*/
cv::imshow( "Display window", image ); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window
return 0;
}
I added a function to test if file exist because I am not sure if result from image.empty() is false due to codec not found or due to path not found. But printing the result prints 1 in any case.
So I am solving 3 things: - how to print working path? - how to make sure if the file was not found because of codec problem (format not recognised) or due to file does not exist? - how to set working path to work in correct path? Currently in project Debuger is set to $(ProjectDir) but is it "project" directory or "project/Debug" or "project/project" or "project/project/Debug"? Seams it should be logicat to set it to "$(ProjectDir)/Debug" - well but this path does not work too!