Basic Manipulation of Images in MATLAB
Basic Manipulation of Images in MATLAB
BASIC MANIPULATION
OF IMAGES IN MATLAB
The objective of this part is to be familiarized with basic read/write operations on images in MATLAB, and learn
the type of images supported as well as some basic operations over them.
Basics
Images have a size in pixels, that we usually express as NxM . In MATLAB, every image is nothing but a matrix.
Each element of the matrix is the gray tone for images in B/W, or, if it is a color image, it can be represented by
three matrixes, where each matrix represents one of the three primary colors (red, green, blue or RGB).
To read an image from disk, we can use imread :
>> f = imread('chestxray.jpg')
>> f = imread('D:\imagenes\chestxray.jpg')
>> f = imread('/home/user/chestxray.jpg')
If the image is B/W, MATLAB will create a matrix f , where each element will have a value of gray:
⎡ f(1, 1) f(1, 2)
http://imgprocessing.tk/intro/images.html f(1, N ) ⎤ 1/5
12/30/2016 Basic manipulation of images in MATLAB
>>size(f)
ans =
1024 1024
We can save the size in a variable:
>>[M, N] = size(f)
We could also save the number of colors of the image with the same command:
>>[M, N, c] = size(f)
And if we want detailed information about the image:
>>whos f
Name Size Bytes Class
f 249x500 373500 uint8 array
Grand total is 373500 elements using 373500 bytes
Displaying images
We may want to display our image or apply effects to it. We can do that with imshow() :
imshow(f,G)
imshow(f,[low high])
For example:
http://imgprocessing.tk/intro/images.html 2/5
12/30/2016 Basic manipulation of images in MATLAB
imshow(f) imshow(f,[0,50])
If we want to expand the dynamic range, we can leave the brackets empty. That will use the minimum intensity
value of the image as the low limit, and the maximum value as the high limit:
imshow(f) imshow(f,[])
Finally, when a new image is loaded, MATLAB overwrites the window of the previously loaded image. To avoid
this, and open a new image in an independent window, do:
>> figure, imshow(f)
Saving images
To save an image:
>> imwrite(f, 'filename')
>> imwrite(f, 'filename', 'tif')
We'll be working mainly with JPEG files, in which case you can also specify the quality:
>> imwrite(f, 'filename.jpg', 'quality', q)
Where q is a number between 0 and 100 (0 = biggest compression, worse quality, 100 = smallest compression, best3/5
http://imgprocessing.tk/intro/images.html
12/30/2016 Basic manipulation of images in MATLAB
Where q is a number between 0 and 100 (0 = biggest compression, worse quality, 100 = smallest compression, best
quality).
To get information about an image:
>> imfinfo filename
would show:
>> imfinfo test.jpg
ans =
Filename: 'test.jpg'
FileModDate: '08‐feb‐2005 17:18:13'
FileSize: 6125
Format: 'jpg'
FormatVersion: ''
Width: 600
Height: 494
BitDepth: 8
ColorType: 'grayscale'
FormatSignature: ''
NumberOfSamples: 1
CodingMethod: 'Huffman'
CodingProcess: 'Sequential'
Comment: {}
Image types
double
Double precision, numbers in floating point that vary in a range of 10308 to 10308 (8 bytes per element)
uint8
8 bit integers in the range of [0,255] (1 byte per element)
uint16
Integers of 16 bits in the range of [0, 65535] (2 bytes per element)
uint32
Integers of 32 bits in the range of [0, 4294967295] (4 bytes per element)
int8
Integers of 8 bits in the range of [128, 127] (1 byte per element)
int16
Integers of 16 bits in the range of [32768, 32767] (2 bytes per element)
int32
Integers of 32 bits in the range of [2147483648,2147483647] (4 bytes per element)
single
Floating point number of simple precision, with values in the range of 1038 to 1038 (4 bytes per element)
char
Characters (2 bytes per element)
logical
The values are 0 or 1 (1 byte per element)
Intensity images
A matrix whose values have been scaled to represent intensity. They could be uint8 or uint16 . If they
are double , the values are scaled in the range [0,1].
Binary images
Images whose values are only 0 or 1. They are represented in MATLAB using logical values. To convert an
array of zeros and ones into a logical array in MATLAB, we type:
>> B = logical(A)
http://imgprocessing.tk/intro/images.html 4/5
12/30/2016 Basic manipulation of images in MATLAB
To check if an array is logical :
>> isLogical(A)
Converting images
Image type conversion table
Command Converts to Valid entry type
For example:
>> f = [0 0.5; 0.75 1.5]
f =
0 0.5000
0.7500 1.5000
>> g = im2uint8(f)
g =
0 128
191 255
>> g = [0 0.3; 0.7 0.9]
g =
0 0.3000
0.7000 0.9000
>> gb = im2bw(g, 0.6)
gb =
0 0
1 1
http://imgprocessing.tk/intro/images.html 5/5