OpenCV cvLoadImage doesn't accept char* for filename but accepts argv[1]
I realise that this question has been asked before. I've read the answers and tried the solution but it hasn't solved it for me.
I'm using OpenCV 2.1 in Ubuntu 10.10(32-bit) and Eclipse C IDE.
My Problem:
If i read a text line from a file, and store it in a char* variable and pass this to cvLoadImage, I get nothing. the text line i read from the file is a fully defined file path to a certain image.
here's the code:
FILE *f = fopen("./input.txt","r");
char img1[50];
fgets(img1,50,f);
char* img3 = strtok(img1,"\n");
IplImage* frame = cvLoadImage(img3);
the result is that frame is now 0x00000000 and no picture
BUT
If I pass the same text as a argument to the executable, i can store argv[1] into a char* and pass that to cvLoadImage() and it reads the image as expected.
here's the code:
char* img3 = argv[1];
IplImage* frame = cvLoadImage(i开发者_如何学编程mg3);
I'm not sure what the cause of this is. :s
the string passed as argument and in file is exactly: (including quotes)
"/home/atharva/Documents/FYP/1a.jpg"
Thanks
You need to remove the quotes from the string in the file. The quotes are only needed for the shell's parser to get the path properly into the program's argv list in the first place -- and even then only really necessary if the file path has embedded spaces.
Since fgets() reads an entire line of text (up to the \n), there's no need to quote anything (although 50 chars isn't much for a file path -- you might want to increase that buffer size). And if it must be quoted in the file for some reason, then you need to remove them before passing it on to cvLoadImage().
精彩评论