使用 openCV Mat c++ 加载图像

2023-01-20C/C++开发问题
2

本文介绍了使用 openCV Mat c++ 加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在 openCV 中使用 Mat 加载图像

I want to load an image using Mat in openCV

我的代码是:

Mat I = imread("C:/images/apple.jpg", 0);
namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I ); 

我在消息框中收到以下错误:

I am getting the following error in a message box:

Unhandled exception at 0x70270149 in matching.exe: 0xC0000005: Access violation 
reading location 0xcccccccc.

请注意,我包括:

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#include <math.h>

推荐答案

我已经讨论过这个之前很多次,我想再次这样做是没有意义的,但是防御性代码:如果一个方法/函数调用可能会失败,请确保您知道它何时发生:

I've talked about this so many times before, I guess it's pointless to do it again, but code defensively: if a method/function call can fail, make sure you know when it happens:

Mat I = imread("C:\images\apple.jpg", 0);
if (I.empty())
{
    std::cout << "!!! Failed imread(): image not found" << std::endl;
    // don't let the execution continue, else imshow() will crash.
}

namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", I ); 
waitKey(0);

请注意,Windows 的路径使用反斜杠 而不是 *nix 系统上使用的标准 /.传递文件名时需要转义反斜杠:C:\images\apple.jpg

Note that Windows' path uses backslash instead of the standard / used on *nix systems. You need to escape the backslash when passing the filename: C:\images\apple.jpg

如果您使用 imshow(),则必须调用 waitKey().

Calling waitKey() is mandatory if you use imshow().

编辑:

如果 cv::imread() 抛出异常我知道唯一可行的解决方案是下载 OpenCV 源代码并在机器上构建它,因为重新- 安装 OpenCV 不能解决问题.

If it's cv::imread() that is throwing the exception the only solution I know to work is downloading OpenCV sources and building it on the machine, since re-installing OpenCV doesn't fix the issue.

这篇关于使用 openCV Mat c++ 加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6