-
- OpenCV中的cv::imdecode函数是从指定的内存缓存中读一幅图像,而cv::imencode是将一幅图像写进内存缓存中。cv::imread是从指定文件载入一幅图像,cv::imwrite是保存一幅图像到指定的文件中。cv::imread和cv::imdecode内部都是通过ImageDecoder类来进行图像解码的。cv::write和cv::encod内部都是通过ImageEncoder类来进行图像编码的
- Python: cv2.imread(filename[, flags]) → retval
Parameters: |
|
The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ). Currently, the following file formats are supported:
- 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 (always supported)
- Sun rasters - *.sr, *.ras (always supported)
- TIFF files - *.tiff, *.tif (see the Notes section)
- Flags的参考值
flag=-1时,8位深度,原通道
flag=0,8位深度,1通道
flag=1, 8位深度 ,3通道
flag=2,原深度,1通道
flag=3, 原深度,3通道
flag=4,8位深度 ,3通道
- 示例代码:
#导入cv模块
import cv2 as cv
import numpy as np
#读取图像,支持 bmp、jpg、png、tiff 等常用格式
#第二个参数是通道数和位深的参数,有四种选择,参考https://www.cnblogs.com/goushibao/p/6671079.html
img = cv.imread("MYD_20140102.tif",2)
print img
#在这里一开始我写成了img.shape(),报错因为img是一个数组不是一个函数,只有函数才可以加()表示请求执行,
#参考http://blog.csdn.net/a19990412/article/details/78283742
print img.shape
print img.dtype
print img.min()
print img.max()
img_reshape=img.reshape(-1,1)
print img_reshape
#创建窗口并显示图像
cv.namedWindow("Image")
cv.imshow("Image",img)
cv.waitKey(0)
#释放窗口
cv.destroyAllWindows()
用这种方法读TIFF出现一个问题,signed 16-bit(-32768~32767)的TIFF无法识别,会读成unsigned 16-bit,值域范围是0~65535.可能是opencv不支持有符号的16bit图像?
- tifffile包
from libtiff import TIFF tif = TIFF.open('filename.tif', mode='r') img = tif.read_image()
但是出现以下问题
TIFFReadDirectory: Warning, Unknown field with tag 33550 (0x830e) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33922 (0x8482) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34735 (0x87af) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34737 (0x87b1) encountered.
但是目前看没什么大碍,好像是未识别tags的问题,日后再探究。