Main Content

imread

Read image from graphics file

描述

example

A= imread(文档名称)从指定的文件中读取图像文档名称, inferring the format of the file from its contents. If文档名称is a multi-image file, thenimreadreads the first image in the file.

A= imread(文档名称,fmt)additionally specifies the format of the file with the standard file extension indicated byfmt。如果imreadcannot find a file with the name specified by文档名称,它寻找名为的文件filename.fmt.

A= imread(___,idx.)从多图像文件中读取指定的图像或图像。此语法仅适用于GIF,PGM,PBM,PPM,CU,ICO,TIF,SV和HDF4文件。您必须指定一个文档名称input, and you can optionally specifyfmt

example

A= imread(___,名称,Value)specifies format-specific options using one or more name-value pair arguments, in addition to any of the input arguments in the previous syntaxes.

example

[A,map] = imread(___)reads the indexed image in文档名称进入Aand reads its associated colormap intomap。Colormap values in the image file are automatically rescaled into the range[0,1]

example

[A,map,transparency] = imread(___)additionally returns the image transparency. This syntax applies only to PNG, CUR, and ICO files. For PNG files,transparency是alpha通道,如果存在一个。对于CUR和ICO文件,它是AND(不透明度)掩码。

例子

collapse all

Read a sample image.

a = imread('ngc6543a.jpg');

imread返回650×600-by-3阵列,A

Display the image.

图像(a)

读取样本索引图像文件中的第一个图像,corn.tif

[x,cmap] = imread('玉米。');

索引图像Xis a 415-by-312 array of typeuint8.。该colormapCMAP.是256×3矩阵类型, therefore there are 256 colors in the indexed image. Display the image.

imshow(X,cmap)

将索引图像转换为RGB图像。结果是415×312×3型类型

RGB = ind2rgb(X,cmap);

Check that values of the RGB image are in the range [0, 1].

DISP(['Range of RGB image is [',num2str(min(rgb(:))),', ',num2str(max(RGB(:))),'].'])
Range of RGB image is [0.0078431, 0.97647].

在示例文件中读取第三个图像,corn.tif

[X,map] = imread('玉米。',3);

Return the alpha channel of the sample image,peppers.png

[X,map,alpha] = imread('peppers.png'); whosalpha
名称大小字节类属性Alpha 0x0 0 Double

No alpha channel is present, soalpha是空的。

Read a specific region of pixels of the sample image,corn.tif

Specify the'PixelRegion'parameter with a cell array of vectors indicating the boundaries of the region to read. The first vector specifies the range of rows to read, and the second vector specifies the range of columns to read.

a = imread('玉米。','PixelRegion',{[1,2],[2,5]});

imreadreads the image data in rows 1-2 and columns 2-5 fromcorn.tifand returns the 2-by-4 array,A

输入参数uments

collapse all

名称of graphics file, specified as a character vector or string scalar.

根据您的文件的位置,文档名称可以承担其中的一个形式。

位置

Form

Current folder or folder on the MATLAB®path

Specify the name of the file in文档名称

Example:'myimage.jpg'

File in a folder

如果the file is not in the current folder or in a folder on the MATLAB path, then specify the full or relative path name.

Example:'C:\myFolder\myImage.ext'

Example:'\ imgdir \ myimage.ext'

URL

如果the file is located by an internet URL, then文档名称必须包含协议类型,如,http://

Example:'http://hostname/path_to_file/my_image.jpg'

Remote Location

如果the file is stored at a remote location, then文档名称must contain the full path of the file specified as a uniform resource locator (URL) of the form:

scheme_name.://path_to_file/my_file.ext.

基于您的远程位置,scheme_name.can be one of the values in this table.

Remote Location scheme_name.
Amazon S3™ S3
Windows Azure®Blob Storage 黄金,草原
HDFS™ HDFS.

For more information, see使用远程数据

Example:'s3://bucketname/path_to_file/my_image.jpg'

For information on the bit depths, compression schemes, and color spaces supported for each file type, seeAlgorithms

数据类型:char|

Image format, specified as a character vector or string scalar indicating the standard file extension. Callimformats.to see a list of supported formats and their file extensions.

Example:'png'

数据类型:char|

阅读图像阅读, specified as an integer scalar or, for GIF files, a vector of integers. For example, ifidx.is 3, thenimread返回文件中的第三个图像。对于GIF文件,如果idx.is1:5, thenimread仅返回前五个框架。该idx.argument is supported only for multi-image GIF, CUR, ICO, and HDF4 files.

When reading multiple frames from the same GIF file, specifyidx.as a vector of frames or use the'Frames','all'名称值对参数。由于GIF文件构造的方式,与调用相比,这些语法提供了更快的性能imread在循环中。

对于HDF4文件,idx.corresponds to the reference number of the image to read. Reference numbers do not necessarily correspond to the order of the images in the file. You can useimfinfoto match image order with reference number.

Example:3

数据类型:

名称-Value Pair Arguments

Specify optional comma-separated pairs of名称,Valuearguments.名称is the argument name andValueis the corresponding value.名称must appear inside quotes. You can specify several name and value pair arguments in any order as名称1,Value1,...,NameN,ValueN

Example:'Index',5读取TIFF文件的第五个图像。
GIF Files

collapse all

读取的框架,指定为逗号分隔对'Frames'and a positive integer, a vector of integers, or'all'。例如,if you specify the value 3,imreadreads the third frame in the file. If you specify'all', thenimreadreads all frames and returns them in the order in which they appear in the file.

Example:'框架',5

JPEG 2000.Files

collapse all

读取的Subimage,指定为逗号分隔的配对组成'PixelRegion'和形式的细胞阵列{Rows,Cols}。该rowsinput specifies the range of rows to read. The科尔斯输入指定要读取的列范围。都rowsand科尔斯must be two-element vectors containing 1-based indices. For example,'PixelRegion',{[1 2],[3 4]}reads the subimage bounded by rows 1 and 2 and columns 3 and 4 in the image data. If the'Reganslevel'值大于0,然后rowsand科尔斯是Subimage的坐标。

Example:'pixelregion',{[1 100],[4 500]}

Reduction of the image resolution, specified as the comma-separated pair consisting of'Reganslevel'and a nonnegative integer. For reduction levelL, the image resolution is reduced by a factor of 2^L。减少水平受到如此规定的分解水平总数的限制'WaveletDecompositionLevels'field in the output of theimfinfo功能。

Example:'Reganslevel',5

数据类型:|

与MATLAB 7.9(R2009B)和更早的兼容性,指定为逗号分隔对'v79申请'and eithertrueorfalse。如果you specifytrue, then the returned grayscale or RGB image is consistent with previous versions ofimread(MATLAB 7.9 (R2009b) and earlier).

Example:'v79申请',true

数据类型:logical

PNG.Files

collapse all

Background color, specified as'none', an integer, or a three-element vector of integers. IfBackgroundColoris'none', thenimreaddoes not perform any compositing. Otherwise,imreadblends transparent pixels with the background color.

  • 如果输入图像被索引,则值为BackgroundColormust be an integer in the range[1,P],在哪里Pis the colormap length.

  • 如果输入图像是灰度,那么值BackgroundColormust be an integer in the range[0,1]

  • 如果the input image is RGB, then the value ofBackgroundColor必须是三元素矢量,其中值在范围内[0,1]

该default value forBackgroundColordepends on the presence of thetransparencyoutput argument and the image type:

  • 如果you request thetransparencyoutput argument, then the default value ofBackgroundColoris'none'

  • 如果you do not request thetransparency输出和PNG文件包含背景颜色块,然后该颜色是默认值BackgroundColor

  • 如果you do not request thetransparencyoutput and the file does not contain a background color chunk, then the default value forBackgroundColoris1for indexed images,0for grayscale images, and[0 0 0]for truecolor (RGB) images.

TIFF Files

collapse all

阅读图像阅读, specified as the comma-separated pair consisting of'Index'and a positive integer. For example, if the value ofIndexis 3, thenimreadreads the third image in the file.

数据类型:|

Information about the image, specified as the comma-separated pair consisting of'Info'and a structure array returned by theimfinfo功能。使用Infoname-value pair argument to helpimreadlocate the images in a multi-image TIFF file more quickly.

数据类型:struct

地区边界, specified as the comma-separated pair consisting of'PixelRegion'和形式的细胞阵列{Rows,Cols}。该rowsinput specifies the range of rows to read. The科尔斯输入指定要读取的列范围。rowsand科尔斯must be either two-element or three-element vectors of 1-based indices. A two-element vector specifies the first and last rows or columns to read. For example,'PixelRegion',{[1 2],[3 4]}reads the region bounded by rows 1 and 2 and columns 3 and 4 in the image data.

A three-element vector must be in the form[start increment stop],在哪里开始is the first row or column to read,increment是一个增量值,和stop是要读取的最后一行或列。此语法允许映像下采样。例如,'PixelRegion',{[1 2 10],[4 3 12]}读取由行1和10和列4和12界限的区域,并在垂直方向上的每2个像素和水平方向上的每3个像素进行样本数据。

Example:'pixelregion',{[1 100],[4 500]}

数据类型:cell

输出参数

collapse all

Image data, returned as an array.

  • 如果the file contains a grayscale image, then A is anm-by-narray.

  • 如果该文件包含索引图像,则A是一个m-by-n索引值数组对应于该索引中的颜色map

  • 如果the file contains a truecolor image, then A is anm-by-n-by-3 array.

  • 如果文件是一个包含彩色图像的TIFF文件that use the CMYK color space, then A is anm-by-n-by-4 array.

该class ofAdepends on the image format and the bit depth of the image data. For more information, seeAlgorithms

ColorMap与索引图像数据相关联A, returned as anm-by-3 matrix of class

透明度信息,作为矩阵返回。对于PNG文件,transparency是alpha通道,如果存在的话。如果没有alpha通道存在,或者指定'BackgroundColor'name-value pair argument, thentransparency是空的。对于cur和iCo文件,transparencyis the AND mask. For cursor files, this mask sometimes contains the only useful data.

More About

collapse all

位深度

比特深度是用于表示每个图像像素的比特数。

Bit depth is calculated by multiplying the bits-per-sample with the samples-per-pixel. Thus, a format that uses 8 bits for each color component (or sample) and three samples per pixel has a bit depth of 24. Sometimes the sample size associated with a bit depth can be ambiguous. For example, does a 48-bit bit depth represent six 8-bit samples, four 12-bit samples, or three 16-bit samples? SeeAlgorithmsfor sample size information to avoid this ambiguity.

Algorithms

collapse all

For most image file formats,imreaduses 8 or fewer bits per color plane to store image pixels. This table lists the class of the returned image array,A,对于文件格式使用的位深度。

位深度in File

Class of Array Returned byimread

每像素1位

logical

2 to 8 bits per color plane

uint8.

9 to 16 bits per pixel

uint16(BMP,JPEG,PNG., andTIFF)

For the 16-bit BMP packed format (5-6-5), MATLAB returnsuint8.

该following sections provide information about the support for specific formats, listed in alphabetical order by format name.

BMP —WindowsBitmap

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths 没有压缩 RLE Compression Output Class 笔记
1位 logical
4 bit uint8.
8 bit uint8.
16位 uint8. 1 sample/pixel
24位 uint8. 3个样本/像素
32位 uint8. 3个样本/像素
(1字节填充)

cur - 光标文件

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths 没有压缩 Compression Output Class
1位 logical
4 bit uint8.
8 bit uint8.

Note

By default, Microsoft®Windows®cursors are 32-by-32 pixels. Since MATLAB pointers must be 16-by-16, you might need to scale your image. If you have Image Processing Toolbox™, you can use theimresize功能。

GIF - 图形交换格式

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths 没有压缩 Compression Output Class
1位 logical
2位to 8 bit uint8.

HDF4 — Hierarchical Data Format

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Raster Image with colormap Raster image without colormap Output Class 笔记
8 bit uint8.
24位 uint8. 3个样本/像素

ICO — Icon File

JPEG — Joint Photographic Experts Group

imreadreads any baseline JPEG image, as well as JPEG images with some commonly used extensions. For information on JPEG 2000 file support, seeJPEG 2000.

Supported Bits per Sample 有损压缩 Lossless Compression Output Class 笔记
8 bit uint8. Grayscale or RGB
12 bit uint16 Grayscale or RGB
16位 uint16 Grayscale

JPEG 2000 - 联合摄影专家组2000年

有关JPEG文件的信息,请参阅JPEG

Note

不支持索引JPEG 2000图像。金宝appJP2 / JPX文件仅支持JP2兼容颜色空格。金宝app缺省情况下,所有图像通道都按照它们存储在文件中的顺序返回。

Supported Bits per Sample

有损压缩 Lossless Compression Output Class 笔记
1位 logical Grayscale only
2位to 8 bit uint8.orINT8. Grayscale
或RGB.
9 bit to 16 bit uint16orint16 Grayscale
或RGB.

PBM — Portable Bitmap

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Raw Binary ASCII (Plain) Encoded Output Class
1位 logical

PCX —Windows画笔

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Output Class 笔记
1位 logical Grayscale only
8 bit uint8. 灰度或索引
24位 uint8. RGB
三个8位样本/像素

PGM — Portable Graymap

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Raw Binary ASCII (Plain) Encoded Output Class 笔记
8 bit uint8.
16位 uint16
Arbitrary 1-bit to 8-bit:uint8.
9-bit to 16-bit:uint16
Values are scaled

PNG.— Portable Network Graphics

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Output Class 笔记
1位 logical Grayscale
2位 uint8. Grayscale
4 bit uint8. Grayscale
8 bit uint8. Grayscale or Indexed
16位 uint16 Grayscale or Indexed
24位 uint8. RGB
三个8位样本/像素。
48位 uint16 RGB
Three 16-bit samples/pixel.

PPM — Portable Pixmap

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Raw Binary ASCII (Plain) Encoded Output Class
最多16位 uint8.
Arbitrary

拉斯 -太阳Raster

This table lists supported bit depths and the data type of the output image data array.

Supported Bit Depths Output Class 笔记
1位 logical Bitmap
8 bit uint8. Indexed
24位 uint8. RGB
三个8位样本/像素
32位 uint8. RGB with Alpha
Four 8-bit samples/pixel

Aperio SVS — AperioScanScope虚拟幻灯片

TIFF-based image file format.imreadsupports reading uncompressed and compressed images, including images with JPEG2000 compression. For more information, seeTIFF — Tagged Image File Format

TIFF — Tagged Image File Format

imreadreads most images supported by the TIFF specification or LibTIFF. Theimreadfunction supports these TIFF capabilities:

  • Any number of samples per pixel

  • CCITT group 3 and 4 FAX, Packbits, JPEG, LZW, Deflate, ThunderScan compression, and uncompressed images

  • Logical, grayscale, indexed color, truecolor and hyperspectral images

  • RGB, CMYK, CIELAB, ICCLAB color spaces. If the color image uses the CMYK color space,A是一个m-by-n-by-4 array. To determine which color space is used, useimfinfoto get information about the graphics file and look at the value of thePhotometricInterpretationfield. If a file contains CIELAB color data,imreadconverts it to ICCLAB before bringing it into the MATLAB workspace. This conversion is necessary because 8-bit or 16-bit TIFF CIELAB-encoded values use a mixture of signed and unsigned data types that cannot be represented as a single MATLAB array.

  • Data organized into tiles or scanlines

imreadreads and converts TIFF images as follows:

  • YCbCr images are converted into the RGB colorspace.

  • All grayscale images are read as if black =0, white = largest value.

  • 1-bit images are returned as classlogical

  • 16-bit floating-point images are returned as class

  • CIELab images are converted into ICCLab colorspace.

出于版权信息,打开libtiffcopyright.txtfile.

XWD - X窗口转储

This table lists the supported bit depths, compression, and output classes for XWD files.

Supported Bit Depths ZPixmaps XYBITMAPS. XYPixmaps Output Class
1位 logical
8 bit uint8.

扩展能力

See Also

||||||(Image Processing Toolbox)

Topics

Introduced before R2006a