You are on page 1of 28

Image processing in Matlab

Week 13
By JI, Hui
Image and pictures
Digital image processing
• Image processing is to change the nature of 
image for
– Type 1: improving its pictorial information for 
human interpretation
– Type 2: rendering it more suitable for autonomous 
machine perception.
• Digital image processing
– involves using a computer to change the nature of 
a digital image
Type 1: Image sharpen
• Enhancing the edges of an image to make it 
appear sharper
Type 1: Image de‐noising
• Removing noise from an image
– Noise: random errors in the image
Type 1: Image de‐blurring
• Removing blurring from an image
Type 2: Edge detection
• For measurement of objects, such as their 
spread, and the area contained within them
Type 2: Image segmentation
• Partitioning  image into interesting regions
Image types and matrices
• Binary
– Each pixel is just black or white
• Greyscale
– Each pixel is a shade of grey, normally 0 (black) to 255 
(white)
• Color
– Here each pixel has a particular colour (~ six millon
colours)
• Indexed
– have a small subset of ~ sixteen million possible 
colours
Binary image
• There are only two possible values 0 or 1 for 
each pixel, we only need one bit per pixel.
• text (printed or handwriting), fingerprints, or 
image edge map
Greyscale image
• Value of each pixel ranging from 0 to 255, can 
be represented by eight bits (one byte)
• Data type: m‐by‐n matrices of uint8 number
Color images (RGB images)
• each pixel has a particular colour; that colour
being described by the amount of red, green 
and blue in it. 
• Bits required for each pixel is 24
• Number of colours:   ଶସ
• Data type: m‐by‐n‐by‐3 matrices of uint8 
number
(cont’)
Index images
• Most colour images only have a small subset of 
the more than sixteen million possible colours. 
• For convenience of storage and file handling, the 
image has an associated colour map, or colour
palette, which is simply a list of all the colours
used in that image
• Each pixel has a value which does not give its 
colour (as for an RGB image), but an index to the 
colour in the map.
(cont’)
Reading images
• im = imread(filename) reading image of 
filename, image type is inferred from its file 
extension
• im = imread(filename,format) reading image 
of filename, image type is given in format
• Imshow(im) shows image im
>>moon = imread('moon.tif');
imshow(moon);
(cont’)
>> im = imread('peppers.png');
>> whos im
Name Size Bytes Class Attributes

im 384x512x3 589824 uint8 imshow(im);


>> imshow(im)
Writing images
• imwrite(im, filename) write an image defined 
by im with name filename, image type is 
inferred from its file extension
>> imwrite(im,'mypeppers.jpg');
>> dir *.jpg

mypeppers.jpg

>>
Data conversion
• Data readed from image is uint8, convert it to 
double before applying some operations
>> moon = imread('moon.tif');
moon=double(moon);

• Before writing data to image files or for 
display, convert it to uint8.
>> moon = moon /2;
imshow(uint8(moon));
Creating images in Matlab
>> black =80*ones(100,100);
>> imshow (uint8(im))

>> I=127*eye(100,100);
>> imshow (uint8(I))
Basic image operations using Matlab
• Image resize
– Change image resolution to reduce image size
>> im = imread('peppers.png');
>> whos im
Name Size Bytes Class Attributes

im 384x512x3 589824 uint8 imshow(im);


>> reim = im(1:2:end,1:2:end,:)
(cont’)

384x512x3 96x128x3
Changing contrast 
• Brighten or darken images
>>imshow(uint8(moon)); >>imshow(uint8(moon));
tmp = moon *2; tmp = moon *1/2+125;
figure;imshow(uint8(tmp)); figure;imshow(uint8(tmp));
Cropping image 
• Only taking a region of image
>> im = imread('peppers.png'); imshow(im);
>> figure; imshow((im(201:end-100,201:end-200,:));
Image rotation
• Rotating image 
>> im = imread('peppers.png'); imshow(im);
>>figure; imshow(im(end:-1:1,:,:));
Color image to greyscale image
• Brightness = 0.2989 * R + 0.5870 * G + 0.1140 
* B 
>> im = imread('peppers.png');
greyim = 0.2989*im(:,:,1) + 0.5870*im(:,:,2)+ 0.1140im(:,:,3);
Image addition

>> I = imread('rice.png');  >> I = imread('rice.png'); 


J = imread('cameraman.tif');  J = imread('cameraman.tif'); 
K=(double(I)+double(J))); K=1/2*(double(I)+1/2*double(J)));
imshow(uint8(K)) imshow(uint8(K))
Image complemention
>> bw = imread('text.png'); bw2 = 1‐bw; 
subplot(1,2,1),imshow(bw);
subplot(1,2,2),imshow(bw2);

You might also like