OpenCV cvLoadImage 不接受 char* 作为文件名,但接受 argv[1]

发布于 2024-10-16 15:10:24 字数 741 浏览 0 评论 0原文

我意识到这个问题以前已经被问过。我已阅读答案并尝试了解决方案,但它没有为我解决问题。

我在 Ubuntu 10.10(32 位)和 Eclipse C IDE 中使用 OpenCV 2.1。

我的问题:

如果我从文件中读取文本行,并将其存储在 char* 变量中并将其传递给 cvLoadImage,我什么也得不到。我从文件中读取的文本行是某个图像的完全定义的文件路径。

这是代码:

FILE *f = fopen("./input.txt","r");
char img1[50];
fgets(img1,50,f);
char* img3 = strtok(img1,"\n");
IplImage* frame = cvLoadImage(img3);

结果是帧现在是 0x00000000 并且没有图片

但是

如果我将相同的文本作为参数传递给可执行文件,我可以将 argv[1] 存储到 char* 中并将其传递给 cvLoadImage() 并读取图像符合预期。

这是代码:

char* img3 = argv[1];
IplImage* frame = cvLoadImage(img3);

我不确定这是什么原因。 :s

作为参数传递并在文件中的字符串正是:(包括引号) “/home/aharva/Documents/FYP/1a.jpg”

谢谢

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(img3);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2024-10-23 15:10:24

您需要从文件中的字符串中删除引号。只有 shell 的解析器首先需要将路径正确地放入程序的 argv 列表中时才需要引号 - 即使这样,只有在文件路径包含嵌入空格的情况下才真正需要。

由于 fgets() 读取整行文本(直到 \n),因此无需引用任何内容(尽管 50 个字符对于文件路径来说并不算多——您可能希望增加缓冲区大小)。如果由于某种原因必须在文件中引用它,那么您需要在将其传递给 cvLoadImage() 之前删除它们。

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文