千家信息网

如何进行OpenCV imread 图片读取

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,如何进行OpenCV imread 图片读取,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。函数原型CV_EXPORTS_W Mat im
千家信息网最后更新 2026年11月01日如何进行OpenCV imread 图片读取

如何进行OpenCV imread 图片读取,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

函数原型

CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );

参数说明

第一个参数是图片路径名称,强烈建议使用/作为目录的分割符号,例如

第二参数具有如下的值,说明了读取过程中是否需要进行像素的操作,例如取值0,表示读取过程中将像素转换为灰度值,默认参数是1, 表示不改变原图像的像素

enum ImreadModes {

IMREAD_UNCHANGED = -1, //!< If set, return the loaded p_w_picpath as is (with alpha channel, otherwise it gets cropped).

IMREAD_GRAYSCALE = 0, //!< If set, always convert p_w_picpath to the single channel grayscale p_w_picpath.

IMREAD_COLOR = 1, //!< If set, always convert p_w_picpath to the 3 channel BGR color p_w_picpath.

IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit p_w_picpath when the input has the corresponding depth, otherwise convert it to 8-bit.

IMREAD_ANYCOLOR = 4, //!< If set, the p_w_picpath is read in any possible color format.

IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the p_w_picpath.

IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert p_w_picpath to the single channel grayscale p_w_picpath and the p_w_picpath size reduced 1/2.

IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert p_w_picpath to the 3 channel BGR color p_w_picpath and the p_w_picpath size reduced 1/2.

IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert p_w_picpath to the single channel grayscale p_w_picpath and the p_w_picpath size reduced 1/4.

IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert p_w_picpath to the 3 channel BGR color p_w_picpath and the p_w_picpath size reduced 1/4.

IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert p_w_picpath to the single channel grayscale p_w_picpath and the p_w_picpath size reduced 1/8.

IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert p_w_picpath to the 3 channel BGR color p_w_picpath and the p_w_picpath size reduced 1/8.

IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the p_w_picpath according to EXIF's orientation flag.

};

例子

#include "opencv2/opencv.hpp"

int main(int argc, char *argv[])

{

cv::Mat srcImg = cv::imread("F:/test2.png", 1);

return 0;

}

注意:

在进行图像的灰度值处理之后,默认情况下, 不会进行图像的直方图进行图像的增强,否则会出现运行异常

代码

#include

#include

#include

using namespace cv;

int main(int argc, char *argv[])

{

Mat p_w_picpath = imread("D:/20170601092226.png", 0);

if (p_w_picpath.empty())

{

std::cout << "打开图片失败,请检查" << std::endl;

return -1;

}

imshow("原图像", p_w_picpath);

waitKey();

return 0;

Mat p_w_picpathRGB[3];

split(p_w_picpath, p_w_picpathRGB);

for (int i = 0; i < 3; i++)

{

equalizeHist(p_w_picpathRGB[i], p_w_picpathRGB[i]);

}

merge(p_w_picpathRGB, 3, p_w_picpath);

imshow("直方图均衡化图像增强效果", p_w_picpath);

waitKey();

return 0;

}

看完上述内容,你们掌握如何进行OpenCV imread 图片读取的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

图片 参数 图像 像素 内容 原图 方法 更多 灰度 直方图 过程 问题 均衡 束手无策 为此 中将 代码 例子 函数 原因 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广东万易互联网科技有限公司惠州分公司 湖南永感互联网科技有限公司 互联网科技公司华为 科技和互联网公司 互联网是不是新科技 互联网科技资迅 互联网医疗科技 贷财互联网科技 壕鑫互联网络科技 甘肃拓世互联网科技有限公司 哈尔滨秒房互联网科技有限公司 深圳前海聚择互联网科技有限公司 互联网科技热点资讯 字节跳动和互联网科技 宜昌元轩互联网科技有限公司 深圳艾果互联网通讯科技 科技大学互联网 互联网金融科技公司的优势 恒生科技指数 中概互联网 数字科技和互联网金融哪个好 互联网科技开发公司 互联网金融科技创新 互联网高科技股票 互联网科技股午盘股票涨跌 健康食品互联网创新科技园项目 河南住房之家互联网科技 互联网科技圈年度人物 上海京穆互联网科技有限公司 珠海东科互联网科技有限公司 银川新华互联网科技学校招聘
0