matlab中imwrite函数详解(imwrite的输出格式)

  参考资料:

  https://www.mathworks.com/help/matlab/ref/imwrite.html?s_tid=srchtitle

  你可能觉得imread函数很简单,但是还是有一些细节要注意。比如我就对imwrite函数输出的图片格式有一些疑问,下面对imwrite函数的用法进行解释,先放一下官方文档:


 

imwrite(A,filename) writes image data 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 type uint8, then imwrite outputs 8-bit values.

  • If A is of data type uint16 and the output file format supports 16-bit data (JPEG, PNG, and TIFF), then imwrite outputs 16-bit values. If the output file format does not support 16-bit data, then imwrite returns an error.

  • If A is a grayscale or RGB color image of data type double or single, then imwrite 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 in A is single, convert A to double before writing to a GIF or TIFF file.

  • If A is of data type logical, then imwrite 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图。 

posted @ 2020-06-12 16:39  思念殇千寻  阅读(4463)  评论(0编辑  收藏  举报