參考資料:
https://www.mathworks.com/help/matlab/ref/imwrite.html?s_tid=srchtitle
你可能覺得imread函數很簡單,但是還是有一些細節要注意。比如我就對imwrite函數輸出的圖片格式有一些疑問,下面對imwrite函數的用法進行解釋,先放一下官方文檔:
imwrite(
writes image data A
,filename
)A
to the file specified by filename
, inferring the file format from the extension. imwrite
creates the new file in your current folder. The bit depth of the output image depends on the data type of A
and the file format. For most formats:
-
If
A
is of data typeuint8
, thenimwrite
outputs 8-bit values. -
If
A
is of data typeuint16
and the output file format supports 16-bit data (JPEG, PNG, and TIFF), thenimwrite
outputs 16-bit values. If the output file format does not support 16-bit data, thenimwrite
returns an error. -
If
A
is a grayscale or RGB color image of data typedouble
orsingle
, thenimwrite
assumes that the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values. If the data inA
issingle
, convertA
todouble
before writing to a GIF or TIFF file. -
If
A
is of data typelogical
, thenimwrite
assumes that the data is a binary image and writes it to the file with a bit depth of 1, if the format allows it. BMP, PNG, or TIFF formats accept binary images as input arrays.
If A
contains indexed image data, you should additionally specify the map
input argument.
imwrite的用法本身也很簡單,A是一個圖像矩陣,從上述說明中可以看出,A的數據類型可以是uint8,uint16,logical等,還可以是indexed image data即索引圖。filename是一個字符串,將輸出圖像的路徑和文件名傳給filename即可。更進一步從描述中可看出,輸出圖片文件的格式由后綴名(extension)決定,uint8基本都支持,uint16則僅有部分圖片格式支持。此時我們就要specific文件名的后綴,防止出現error。
看文檔又好奇這個indexed image即索引圖是什么,在網上找了一張圖:
簡單來說就是給一幅圖像出現的所有RGB值編一個映射表,然后有一個和圖像形狀一樣的索引矩陣,查表即可得到RGB圖。