【问题标题】:cvLoadImage is returning int?cvLoadImage 正在返回 int?
【发布时间】:2018-02-05 13:24:34
【问题描述】:

根据 OpenCV 文档,cvLoadImage 必须返回指向 IplImage 的指针,但看起来它正在返回 int。

当我运行下面的程序时,我收到了一些如下所示的警告

#include "highgui.h"
#include <stdio.h>

int main(int argc, char** argv)
{
    IplImage *img = cvLoadImage ("/path/to/file/face.jpg");
    if(img != NULL)
            printf("%d", img->width);
}

当我运行上面的代码时,我也遇到了分段错误,我猜是因为 img 是一个 int,所以当我尝试访问 img->width 时它会导致崩溃,编译上面的代码时会出现警告

/usr/local/Cellar/opencv/3.3.0_3/include/opencv2/core/types_c.h:929:13: warning: 
      implicit declaration of function 'cvRound' is invalid in C99
      [-Wimplicit-function-declaration]
    ipt.x = cvRound(point.x);
            ^
    main.c:7:21: warning: implicit declaration of function 'cvLoadImage' is invalid
          in C99 [-Wimplicit-function-declaration]
        IplImage *img = cvLoadImage ("/path/to/file/face.jpg");
                        ^
    main.c:7:15: warning: incompatible integer to pointer conversion initializing
          'IplImage *' (aka 'struct _IplImage *') with an expression of type 'int'
          [-Wint-conversion]
        IplImage *img = cvLoadImage ("/path/to/file/face.jpg");

警告说 int 的转换不兼容,所以我将 IplImage 更改为 int,它工作正常,它为 img 输入了一些负整数值

#include "highgui.h"
#include <stdio.h>

int main(int argc, char** argv)
{
    printf("%s\n","hello world");
    int img = cvLoadImage ("/path/to/file/face.jpg");
    printf("%d", img);
}

我收到上述程序的以下警告

implicit declaration of function 'cvRound' is invalid in C99
      [-Wimplicit-function-declaration]
    ipt.x = cvRound(point.x);
            ^
main.c:7:15: warning: implicit declaration of function 'cvLoadImage' is invalid
      in C99 [-Wimplicit-function-declaration]
    int img = cvLoadImage ("/path/to/file/face.jpg");

睡不着想明白了,google也帮不上忙,是OpenCV版本还是C版本的问题?,我用的是Opencv2 3.3.0,请问需要什么资料

【问题讨论】:

  • 您缺少一个头文件(函数的前向声明)。
  • @Sourav Ghosh,可能是但不知道是哪一个
  • 您是否尝试添加cv.h
  • 是的,仍然收到隐式声明警告,我之前尝试过
  • 在 3.3.0 版本中,cvLoadImage 不在 highgui 模块中,它在 imgcodecs 中 -- 可能是 #include "opencv2/imgcodecs/imgcodecs_c.h"?

标签: c opencv


【解决方案1】:

第一个代码没有问题,如果使用 g++ 而不是 gcc 编译,应该可以正常运行。

您从IplImage* 隐式转换为int 的第二个代码是错误的并且不会运行。

关于 cvRound() 函数的警告和错误与 OpenCV C API 有关,该 API 有一段时间没有更新,很可能会在未来的版本中被删除。大多数新的 OpenCV 功能仅存在于 C++ API 中。

其他提到 C API 中 cvRound() 函数错误的帖子:

https://github.com/opencv/opencv/issues/6076

https://github.com/opencv/opencv/issues/8438

https://github.com/opencv/opencv/issues/8658

将来尝试使用 OpenCV C++ API 以获得最佳结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多