imread

Reads an image file into an array.

Category: Data Import and Analysis > Data Import and Export

Syntax

A = imread(filename)

Reads the image. Grayscale gives an m-by-n array, color an m-by-n-by-3 array.

[A, map] = imread(filename)

Also returns the colormap of an indexed image.

Description

The class of A follows the bit depth of the file, so convert with im2double before arithmetic that expects values in 0 to 1. Rows run from top to bottom and the first color plane is red.

Use imshow to display the array and imfinfo to inspect the file without loading it.

Input arguments

filename
Image file name. PNG, JPEG, BMP and TIFF files are supported.

Output arguments

A
Pixel data, usually uint8, or uint16 for 16-bit files; logical for binary images.
map
Colormap of an indexed image, empty otherwise.

Example

Round trip through PNG

RGB = zeros(2, 2, 3, 'uint8');
RGB(:, :, 1) = 255;
RGB(1, 1, :) = [0 255 0];
imwrite(RGB, 'c.png');
K = imread('c.png');
size(K)
class(K)
isequal(K, RGB)
squeeze(K(1, 1, :))'
Output
ans = 1×3

     2     2     3
ans = 'uint8'
ans = logical
   1
ans = 1×3 uint8 row vector

     0   255     0

See also

imwrite imfinfo imshow im2double