前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opencv的imread函数_opencv imwrite

opencv的imread函数_opencv imwrite

作者头像
全栈程序员站长
发布2022-11-04 15:31:18
5860
发布2022-11-04 15:31:18
举报
文章被收录于专栏:全栈程序员必看

近日,开始学习图像处理,思前想后决定以opencv作为实验基础。遂完成图片读取和显示功能。Imread作为常用的图像读取函数,虽然简单,但是参数的选择非常重要,直接影响到后期处理。同时在调试学习过程中也可以学习到图像处理的知识。

0 函数原型

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

返回Mat对象;

参数filename: 待打开图片的绝对地址,需要注意的是,并不是所有文件都可以用它打开,它支持的文件如下;函数识别不是依靠文件的后缀名,而是依靠内容的编码格式;

需要注意的是imread读取数据时会重新排列数据。

  • Windows bitmaps – *.bmp, *.dib (always supported)
  • JPEG files – *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files – *.jp2 (see the Notes section)
  • Portable Network Graphics – *.png (see the Notes section)
  • WebP – *.webp (see the Notes section)
  • Portable image format – *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported)
  • Sun rasters – *.sr, *.ras (always supported)
  • TIFF files – *.tiff, *.tif (see the Notes section)
  • OpenEXR Image files – *.exr (see the Notes section)
  • Radiance HDR – *.hdr, *.pic (always supported)
  • Raster and Vector geospatial data supported by Gdal (see the Notes section)

参数flags:打开的参数,这个非常重要,因为如果设置不合适的话,很容易出现预想之外的效果。它可以指导将原图读取时进行一定的转换。默认值是IMREAD_LOAD_GDAL。因此,如果是想直接处理原图,应该设置为IMREAD_UNCHANED。

IMREAD_UNCHANGED

If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).

IMREAD_GRAYSCALE

If set, always convert image to the single channel grayscale image.

IMREAD_COLOR

If set, always convert image to the 3 channel BGR color image.

IMREAD_ANYDEPTH

If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.

IMREAD_ANYCOLOR

If set, the image is read in any possible color format.

IMREAD_LOAD_GDAL

If set, use the gdal driver for loading the image.

IMREAD_REDUCED_GRAYSCALE_2

If set, always convert image to the single channel grayscale image and the image size reduced 1/2.

IMREAD_REDUCED_COLOR_2

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.

IMREAD_REDUCED_GRAYSCALE_4

If set, always convert image to the single channel grayscale image and the image size reduced 1/4.

IMREAD_REDUCED_COLOR_4

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.

IMREAD_REDUCED_GRAYSCALE_8

If set, always convert image to the single channel grayscale image and the image size reduced 1/8.

IMREAD_REDUCED_COLOR_8

If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.

1 通道编码顺序

通道,与像素深度深度有关。灰度图通常是8比特的像素深度,则通道数为1。如果是彩色图,且为RGB编码,那么一般为24比特的像素深度,通道数为3。而有的彩色图的像素深度是16或者32比特。16比特可能有多种情况:一是压缩的RGB格式,二是YUV的输出。无论何种,都是只有2通道,需要手动解析分离。32比特(windows *.bmp)的像素深度对应的彩色图,则表示的是4通道,RGBA,多出的A表示的是透明度的索引。

另外读取时需要注意内部像素的编码顺序,这也依赖于imread的flags选项的取值,如果取值决定转成RGB,那么正常的顺序是BGR,排列顺序如下图所示。如果最后imread输出是四通道,多了Alpha通道,那么顺序是RGBA。

opencv的imread函数_opencv imwrite
opencv的imread函数_opencv imwrite

2 图像像素通道数据访问

这部分可以借鉴网上资料,可以分为三种类型。

2.1 动态访问at<typename>(i,j)

Imread返回的mat类,提供了at模板函数。Image.at<uchar>(i, j);取出i行j列的数据,uchar可以理解为imread返回之后图像的编码类型(如1所述的通道)。如果是三通道,则可以是Vec3b,四通道则是Vec4b。

//CV_LOAD_IMAGE_UNCHANGED如果要取A分量那么flag最好设置成这个值 Mat image = imread(“1_firstlai.png”, CV_LOAD_IMAGE_UNCHANGED); for(int i=0;i<image.rows;i++) { for(int j=0;j<image.cols;j++) { image.at<Vec3b>(i,j)[0]; //B image.at<Vec3b>(i,j)[1]; //G image.at<Vec3b>(i,j)[2]; //R image.at<Vec4b>(i,j)[0]; //B image.at<Vec4b>(i,j)[1]; //G image.at<Vec4b>(i,j)[2]; //R image.at<Vec4b>(i,j)[3]; //A } }

2.2 指针-更加高效

imgage.ptr<uchar>(i)。

int nr=image.rows; // 将3通道转换为1通道 int nl=image.cols*image.channels(); for(int k=0;k<nr;k++) { // 每一行图像的指针 const uchar* inData=image.ptr<uchar>(k); for(int i=0;i<nl;i++) { inData[i]; } }

本质就是将每行的3/4通道数据转换为1通道数据访问,因为OpenCV内部存储每一行像素数据以及像素内部通道数据都是连续存储的。但是行与行的数据并不一定是连续存储的,所以不能应用在行与行之间。

2.3 结合isContinuous的指针

2.2中已经说明了,OpenCV中行与行之间不一定连续存储,也就是有可能连续存储,而且提供了对应的API支持判断是否连续这一现象,也可基于此,再提高访问速度。

int nr=image.rows; int nc=image.cols*image.channels(); if(image.isContinuous()){ nc=nc*nr; nr=1; } for(int i=0;i<nr;i++){ // 每一行图像的指针 const uchar* inData=image.ptr<uchar>(i); for(int j=0;j<nc;j++){ inData[j]; } }

2.4 安全但低效的迭代器

2.1-2.3的方法虽然效率高,但是如果操作不小心,容易造成数组越界的Bug。所以opencv提供了一种更安全的访问方法-迭代器。

MatIterator_<Vec3b> it_im, itEnd_im; it_im = im.begin<Vec3b>(); itEnd_im = im.end<Vec3b>(); for (; it_im != itEnd_im; it_im++, it_om++){ (*it_im)[0] ; //B (*it_im)[1] ; //G (*it_im)[2] ; //R }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/182081.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 近日,开始学习图像处理,思前想后决定以opencv作为实验基础。遂完成图片读取和显示功能。Imread作为常用的图像读取函数,虽然简单,但是参数的选择非常重要,直接影响到后期处理。同时在调试学习过程中也可以学习到图像处理的知识。
    • 0 函数原型
      • 1 通道编码顺序
        • 2 图像像素通道数据访问
          • 2.1 动态访问at<typename>(i,j)
          • 2.2 指针-更加高效
          • 2.3 结合isContinuous的指针
          • 2.4 安全但低效的迭代器
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档