1、imread()
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_COLOR。因此,如果是想直接处理原图,应该设置为IMREAD_UNCHANED。
————————————————
版权声明:本文为CSDN博主「时行居正」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/firstlai/article/details/70882240
opencv 3.4.2版本说明文档:https://docs.opencv.org/3.4.2/
enum cv::ImreadModes {
cv::IMREAD_UNCHANGED = -1, 若有α值,则会导入,否则会丢掉α值。
cv::IMREAD_GRAYSCALE = 0, 读取单通道的灰度图像。
cv::IMREAD_COLOR = 1, 读取3通道的BGR图像。
cv::IMREAD_ANYDEPTH = 2, 读取为与原图像的深度对应的16bit/32bit的图像,否则转换为8bit。
cv::IMREAD_ANYCOLOR = 4, 读取为任意的彩色格式
cv::IMREAD_LOAD_GDAL = 8, 利用GDAL来载入图像
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
}
2、显示图像
cv2.imshow('显示窗口名“,显示的对象)
cv2.waitKey(毫秒) 等待特定的几毫秒接收键盘输入,无输入返回-1;0毫秒会一直等下去
cv2.destroyAllWindows()
cv2.destroyWindow("要删去的窗口名")
如果想要打开的窗口可手动调整大小:
cv2.nameWindow('窗口名',cv2.WINDOW_NORMAL)
cv2.imshow('窗口名',显示对象)
3、保存图像
cv2.imwrite('文件名',对象)
4、总结
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 27 10:54:28 2019
@author: 不愿透露姓名的美凌格
"""
import cv2
img = cv2.imread('lena512color32.bmp',-1)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27: #wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('s'): #wait for 's' key to save and exit
cv2.imwrite('lena512color24.png',img)
cv2.destroyAllWindows()