python學習之讀取TIFF
阿新 • • 發佈:2019-01-02
-
- 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的問題,日後再探究。