OpenCV中函数imread的参数flags的枚举值(可设置值)及其具体意义
Posted 昊虹图像算法
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV中函数imread的参数flags的枚举值(可设置值)及其具体意义相关的知识,希望对你有一定的参考价值。
imread函数的原型如下:
C++:
Mat cv::imread(const String & filename,int flags = IMREAD_COLOR)
Python:
retval=cv.imread(filename[, flags])
第二个参数flags是一个可选参数,默认值为“ IMREAD_COLOR”其枚举值(可设置值)的具体意义如下:
上面这张截图来自网页:
https://docs.opencv.org/4.1.2/d4/da8/group__imgcodecs.html#ga61d9b0126a3e57d9277ac48327799c80
具体的枚举值如下:
enum cv::ImreadModes
cv::IMREAD_UNCHANGED = -1,
cv::IMREAD_GRAYSCALE = 0,
cv::IMREAD_COLOR = 1,
cv::IMREAD_ANYDEPTH = 2,
cv::IMREAD_ANYCOLOR = 4,
cv::IMREAD_LOAD_GDAL = 8,
cv::IMREAD_REDUCED_GRAYSCALE_2 = 16,
cv::IMREAD_REDUCED_COLOR_2 = 17,
cv::IMREAD_REDUCED_GRAYSCALE_4 = 32,
cv::IMREAD_REDUCED_COLOR_4 = 33,
cv::IMREAD_REDUCED_GRAYSCALE_8 = 64,
cv::IMREAD_REDUCED_COLOR_8 = 65,
cv::IMREAD_IGNORE_ORIENTATION = 128
下面一个参数一个参数的说。
IMREAD_UNCHANGED
Python: cv.IMREAD_UNCHANGED
If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
设置这个参数表示原样读取图像,即使图像有alpha通道(alpha通道用于控制图像的透明度),读取时也会保留,否则不会保留alpha通道
IMREAD_GRAYSCALE
Python: cv.IMREAD_GRAYSCALE
If set, always convert image to the single channel grayscale image (codec internal conversion).
设置这个参数表示把图像转换成灰度图像后读取。
IMREAD_COLOR
Python: cv.IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
设置这个参数表示将图像转换成3通的BGR彩色图像。
IMREAD_ANYDEPTH
Python: cv.IMREAD_ANYDEPTH
If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
设置这个参数表示16位或32位深度的图像在读取时也按16位或32位深度的图像读取,如果没有选择这个参数,则会转换为8位深度的图像。
IMREAD_ANYCOLOR
Python: cv.IMREAD_ANYCOLOR
If set, the image is read in any possible color format.
设置这个参数,那么图像将以任意的彩色格式被读取。
IMREAD_LOAD_GDAL
Python: cv.IMREAD_LOAD_GDAL
If set, use the gdal driver for loading the image.
设置这个参数,则在读取图像时会使用GDAL驱动加速图像的读取。
PS:GDAL-Geospatial Data Abstraction Library 是使用C/C++语言编写的用于读写空间数据的一套跨平台开源库。简单地说,GDAL是一个操作各种栅格地理数据格式的库。包括读取、写入、转换、处理。
IMREAD_REDUCED_GRAYSCALE_2
Python: cv.IMREAD_REDUCED_GRAYSCALE_2
If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
设置这个参数,读取图像时会将图像转换成灰度图并将图像尺寸缩小为原来的1/2
IMREAD_REDUCED_COLOR_2
Python: cv.IMREAD_REDUCED_COLOR_2
If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
设置这个参数,读取图像时会将图像转换成BGR三通道图像,并且尺寸缩小为原来的1/2
IMREAD_REDUCED_GRAYSCALE_4
Python: cv.IMREAD_REDUCED_GRAYSCALE_4
If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
设置这个参数,读取图像时会将图像转换成灰度图并将图像尺寸缩小为原来的1/4
IMREAD_REDUCED_COLOR_4
Python: cv.IMREAD_REDUCED_COLOR_4
If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
设置这个参数,读取图像时会将图像转换成BGR三通道图像,并且尺寸缩小为原来的1/4
IMREAD_REDUCED_GRAYSCALE_8
Python: cv.IMREAD_REDUCED_GRAYSCALE_8
设置这个参数,读取图像时会将图像转换成灰度图并将图像尺寸缩小为原来的1/8
IMREAD_REDUCED_COLOR_8
Python: cv.IMREAD_REDUCED_COLOR_8
设置这个参数,读取图像时会将图像转换成BGR三通道图像,并且尺寸缩小为原来的1/8
IMREAD_IGNORE_ORIENTATION
Python: cv.IMREAD_IGNORE_ORIENTATION
If set, do not rotate the image according to EXIF’s orientation flag.
设置这个参数,不会根据EXIF格式图像的方向设置旋转图像。
Python示例代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 图像处理开发需求、图像处理接私活挣零花钱,请加微信/QQ 2487872782
# 图像处理开发资料、图像处理技术交流请加QQ群,群号 271891601
import cv2 as cv
img01 = cv.imread('./image/food-01.jpg') # 不设置第二个参数,即取默认值“IMREAD_COLOR”
img02 = cv.imread('./image/food-01.jpg', cv.IMREAD_UNCHANGED)
img03 = cv.imread('./image/food-01.jpg', cv.IMREAD_GRAYSCALE)
img04 = cv.imread('./image/food-01.jpg', cv.IMREAD_COLOR)
img05 = cv.imread('./image/food-01.jpg', cv.IMREAD_ANYDEPTH)
img06 = cv.imread('./image/food-01.jpg', cv.IMREAD_ANYCOLOR)
img07 = cv.imread('./image/food-01.jpg', cv.IMREAD_LOAD_GDAL)
img08 = cv.imread('./image/food-01.jpg', cv.IMREAD_REDUCED_GRAYSCALE_2)
img09 = cv.imread('./image/food-01.jpg', cv.IMREAD_REDUCED_COLOR_2)
img10 = cv.imread('./image/food-01.jpg', cv.IMREAD_REDUCED_GRAYSCALE_4)
img11 = cv.imread('./image/food-01.jpg', cv.IMREAD_REDUCED_COLOR_4)
img12 = cv.imread('./image/food-01.jpg', cv.IMREAD_REDUCED_GRAYSCALE_8)
img13 = cv.imread('./image/food-01.jpg', cv.IMREAD_REDUCED_COLOR_8)
img13 = cv.imread('./image/food-01.jpg', cv.IMREAD_IGNORE_ORIENTATION)
以上是关于OpenCV中函数imread的参数flags的枚举值(可设置值)及其具体意义的主要内容,如果未能解决你的问题,请参考以下文章