Ask Your Question
0

Why my program cannot open image? How show working path? VS2010

asked 2016-06-04 04:14:13 -0600

VanGog gravatar image

updated 2016-06-04 04:31:22 -0600

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 ... problem with this image
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 );
cv::imshow( "Display window", image );
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!
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-04 06:08:41 -0600

pklab gravatar image

please ask only OpenCV related question, The answers to your question are here:

  • google with "C++ check if a file exists"
  • google with "C++ PrintFullPath"
  • google with "C++ Visual Studio Debug Working Directory"

BTW the debugger runs form the path as defined in Project Properties > Configuration Progerties > Debugging > Working Directory. I suggest to configure that with $(TargetDir) to be sure that the debuggers runs from same path where is your executable.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-06-04 04:14:13 -0600

Seen: 1,075 times

Last updated: Jun 04 '16