Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV cvLoadImage function doesn't work with explicit paths

Tags:

opencv

I am new to using OpenCV and have run into an issue when I try to load an image using the cvLoadImage() function.

The following code attempts to load the image "testImg.bmp" using (1) the working directory (the commented-out line of code) or (2) the explicit path to the file on the desktop. Note that the two image files are the exact same except that they are located in two locations (i.e. the working path is not the desktop).

When I run the program using the first line (the commented-out line) the image is loaded and displays in a window. When I run the program as written (i.e. using the explicit path to the image on the desktop), however, an empty window pops up and the error message pops out (the "Unable to load image" message).

Any help towards telling me what I have done wrong with the explicit path line is greatly appreciated as moving the image file to the working directory is, in general, not feasible/desirable.

#include "stdafx.h"

#include "opencv/cv.h"
#include "opencv/highgui.h"

using namespace std; 

int main(int argc, CHAR* argv[])
{
    //IplImage* img = cvLoadImage("testImg.bmp");
    IplImage* img = cvLoadImage("C:\Users\Jeremy\Desktop\testImg.bmp");

    if(!img)
        std::cerr<<"Unable to load image"<<endl;

    cvNamedWindow("myfirstwindow");
    cvShowImage("myfirstwindow",img);

    cvWaitKey(0);
    cvReleaseImage(&img);
    return 0;
}
like image 706
user1457356 Avatar asked Jul 30 '26 05:07

user1457356


1 Answers

It's nothing to do with opencv

"\" in a C string means that the next char is an escape code, use either "\\" or "/"

like image 151
Martin Beckett Avatar answered Aug 05 '26 16:08

Martin Beckett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!