3

我是 OpenCV 的初学者。我的编程环境是 VC++ Express 2010 + OpenCV 2.4.2 + Win 7 64 位。

我在我的 VC++ 和 Path 中使用纯 32 位配置。

我输入以下代码:

#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
using namespace cv;

int main(int argc, char** argv) {
    char* imgPath = "logo.png";
    Mat img = imread(imgPath);
    namedWindow( "Example1", WINDOW_AUTOSIZE);
    imshow("Example1", img);
    waitKey(0);
    return 0;
}

然后我编译并运行。它确实提出了一个窗口(但没有图片),但后来给了我这个(运行时错误?)

Unhandled exception at 0x770515de in Helloworld2.exe: Microsoft C++ exception: cv::Exception at memory location 0x001ef038..

然后我将 imread 更改为 cvLoadImage 并且它可以正常工作。

有人可以告诉我有什么问题吗?

4

5 回答 5

2

我已经尝试过您提供的代码。它与我安装的 OpenCV 完美配合。

但是,我在线收到警告:

char* imgPath = "logo.png";

main.cpp:6:21: warning: deprecated conversion from string constant to 'char*' [-

写字符串]

我认为这不会导致代码崩溃,但这可能是您的问题,因为我没有使用 VC++ 进行编译。

您可以尝试检查这是否是问题是imgPath直接替换为字符串,所以代码现在就像

Mat img = imread("logo.png"); 
于 2012-08-16T16:23:15.440 回答
1

我也遇到了这个问题,但我用以下方法解决了这个问题。重点是使用相对路径以外的绝对路径,将文件路径中的“\”改为“/”。

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

int main() {
    cv::Mat image = cv::imread("D:/projects/test/Debug/1.jpg");
    if (image.empty()) {
        return 0;
    }
    cv::namedWindow("my image");
    cv::imshow("my image", image);
    cv::waitKey(5000);
    return 1;
}
于 2013-11-06T10:01:07.330 回答
0

cvLoadImage和之间的区别imread可以在opencv文档中看到:

C++: Mat imread( const string& filename , int flags=1 )

C: CvMat* cvLoadImageM( const char* 文件名, int iscolor=CV_LOAD_IMAGE_COLOR )

但是有一个从const char *to的隐式转换string。正如 masad 所指出的,这种转换已被弃用,因此它非常依赖于编译器。

cvLoadImage您工作,您似乎应该将代码更改为以下内容:

#include "opencv2\highgui\highgui.hpp"
#include "opencv2\core\core.hpp"
#include <string>
using namespace cv;

int main(int argc, char** argv) {
    std::string imgPath("logo.png");
    Mat img = imread(imgPath);
    namedWindow( "Example1", WINDOW_AUTOSIZE);
    imshow("Example1", img);
    waitKey(0);
    return 0;
}

Visual Studio 中的 C++ 接口存在一些问题,但您可以尝试看看它是否适合您。

于 2012-08-17T11:14:19.413 回答
0

我遇到了同样的问题,我遇到了在 Visual C++ 2010 Express 中安装 OpenCV 2.4.3,它提到在为链接器添加依赖项时使用更新的库集 *d.lib。我试过了,C++ 接口工作了。不确定这是否是一般情况。我在 64 位 Windows 机器上使用 OpenCV 2.4 和 Visual Studio 2010。

于 2013-01-24T23:28:12.903 回答
0
于 2013-11-16T14:23:40.407 回答