OpenCV图像读取与灰度化
来源:互联网 发布:聚划算与淘宝的关系 编辑:程序博客网 时间:2024/06/05 02:57
在OpenCV中,图像的读取可以通过imread()函数实现:
Mat imread( const String& filename, int flags = IMREAD_COLOR );
读取后的数据存储格式由第二个形参flags决定。flag的值可以从枚举类型cv::ImreadModes中选取:
enum ImreadModes { IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image. IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image. IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit. IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format. IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image. IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2. IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2. IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4. IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4. IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8. IMREAD_REDUCED_COLOR_8 = 65 //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.};
常用的读取格式有 IMREAD_GRAYSCALE 和 IMREAD_COLOR。按照IMREAD_GRAYSCALE 方式读取,得到的结果为CV_8U类型(8位无符号整型 0~255)的单通道灰度值, IMREAD_COLOR读取的结果是按照B G R 顺序存储的CV_8U类型三通道色彩图。(注意色彩通道的存储顺序!!!)
灰度化的过程可以通过cvtColor()函数实现:
void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );
src, dst 分别表示源图像和灰度化后存储的目的图像(Mat);
dstCn 表示输出图像的通道数,默认值0表示跟输入输入图像的通道数保持一致;
code 表示转换的方式,它可以实现在多种颜色空间的格式直接进行转换。详见ColorConversionCodes.
一般code的值为CV_**BGR**2GRAY,表示从三通道色彩图转化为灰度图。
转换过程按照如下公式进行:
RGB[A] to Gray:Y←0.299⋅R+0.587⋅G+0.114⋅B
注意上文提到色彩图的通道的存储顺序为B G R,所以 这里是BGR2GRAY。而CV_RGB2GRAY也是可取的值,它当然是在图像通道顺序为RGB时使用了。二者之间的区间就在于告诉上面的计算公式每个点像素值是按照什么顺序排列的。
更多关于色彩空间的转换,详见官方文档color conversion.
- OpenCV图像读取与灰度化
- OpenCV读取灰度图像
- OpenCV 读取灰度图像的灰度值
- OpenCV图像灰度化
- OpenCV图像灰度化
- OpenCV灰度化图像
- OpenCV 读取文件并转换灰度图像
- opencv读取灰度图像像素代码
- OpenCV读取yuv420对应的灰度图像
- opencv彩色图像与灰度图像叠加
- Opencv-图像灰度化、二值化
- opencv mat 图像灰度化
- python opencv(9)图像的复制与灰度化
- 图像基础、OpenCV入门3——图像灰度化、二值化与图像加噪
- 灰度与灰度图像
- 灰度与灰度图像
- Opencv读取文件夹连续图片,RGB分量显示,图像灰度化
- Opencv读取文件夹连续图片,RGB分量显示,图像灰度化
- linux编辑Authority文件时如何调到行尾或行首
- 这哪是NBA球场!简直就是黑科技实验室啊!
- CRC校验算法
- MFC常用DC相关类的作用
- 大端小端(字节序)
- OpenCV图像读取与灰度化
- 数据结构---线性表---顺序存储
- 问题 A(1301): 湖的深度
- python异步并发模块concurrent.futures简析
- 'QString::QString(const char*)' is qstring.h private的原因和解决
- Java 抽象类
- gitLab的安装
- 如何使用微信、支付宝提现免手续费
- c++算法之回溯 八皇后延伸 N皇后问题