0% found this document useful (0 votes)
7 views34 pages

Fisrt steps in image processing

This document provides an introduction to image processing using MATLAB, covering essential functions such as image display, importing images, and performing arithmetic and logical operations on images. It explains how images are represented as matrices of pixels and discusses various color models and palettes used in MATLAB for image display. Additionally, it includes code examples for importing raw images and performing logical operations between images.

Uploaded by

Red Knight
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views34 pages

Fisrt steps in image processing

This document provides an introduction to image processing using MATLAB, covering essential functions such as image display, importing images, and performing arithmetic and logical operations on images. It explains how images are represented as matrices of pixels and discusses various color models and palettes used in MATLAB for image display. Additionally, it includes code examples for importing raw images and performing logical operations between images.

Uploaded by

Red Knight
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Fisrt steps in image processing

This lecture provides the reader with a few


elements on image processing with MATLAB ,
which comes equipped with 2D (two dimension)
functions, necessary when working in this field, a
field not too different from ID signal processing.

– Image display, color palette


– Importing images
– Arithmetical and logical operations
Image display, color palette
An image is considered as a set of pixels (pixel:
picture element), associated with a rectangular
grid of the original image.

Fig.1 Each point of the original image has an 8-bit coded


"gray-level". Each pixel appears as a gray square
In MATLAB®, there are several ways to display an
image:
- Either directly with an (TV x M x 3) or (TV x M x
4) array depending on the color model: RGB
(Red, Green and Blue), CMYK (Cyan, Magenta,
Yellow and BlacK), HSL (Hue, Saturation and
Lightness), CIE Lab ("Commission Internationale
de l'Eclairage": L is for luminance, a and b are
color component coordinates), etc.
In the following example, an image in JPEG
format is imported with the use of the imread
function as a 3 dimension 800 x 580 x 3 array, the
3 indicating that there are three RGB color
planes. Notice that the data type used is the 8-bit
unsigned integer:
>> xx=imread('Kohno_IP','jpeg');
>> whos
Name Size Bytes Class Attributes

image1 2x3 48 double


xx 1152x1728x3 5971968 uint8
— either by using a 2D (short for 2 dimension) array
and a color palette. This is the display mode we
will be using; it is called an indexed
representation.

Let A = [a(2, j)], with 1 < i < N and 1 < j < M, be an N


x M array. The number a(i,i), placed in line i and
column j, indicates the color of the point with
coordinates (i,j) in the image after it has been
"sampled" and "quantified". The line index i
represents the horizontal position, and the column
index j represents the vertical position. The point
with the coordinates (1, 1) is placed in the top-left
corner
>> imagel=[32 0 48; 0 16 0] ;
>> image (imagel) ; colormap('gray')

Fig.2 The image displayed is comprised of 6 points, or logical pixels, and


the one associated with image1(1, 1) is the one in the top-left corner
Notice that an element of the array with the index
(i,j) can be associated with several physical pixels of
the display window. In fact, there is no reason for
the number of values of the matrix of elements
a(i, j) to be equal to the number of physical pixels
of the display window. Hence, a point with the
coordinates (i,j) can be represented by several
physical pixels, just as a physical pixel can be used
to represent several points with the coordinates (i,j).
Notice that an element of the array with the
index (i,j) can be associated with several physical
pixels of the display window. In fact, there is no
reason for the number of values of the matrix of
elements a(i, j) to be equal to the number of
physical pixels of the display window. Hence, a
point with the coordinates (i,j) can be represen-
ted by several physical pixels, just as a physical
pixel can be used to represent several points with
the coordinates (i,j).
If we want to display a Figure and preserve its real
size (one screen pixel corresponding to one image
pixel), we will be using the properties units,
Position, AspectRatio... (these parameters can
change from one MATLAB version to the next).

>> set (gca, 'units' , 'pixels ', 'Position' , [20 20 fliplr(size (imagel))] )
• In the indexed representation, a(i,j) indexes a
color array called the palette (Figure 3). The
color palette is a (P x 3) array where each line
is used to code a color according to its Red,
Green and Blue components (RGB) using a
real number between 0 and 1.
This representation is convenient since most
bitmap editing programs can provide an image
description in three planes, each one of them
corresponding to a primary color R, G or B,
encoded as an integer between 0 and 2n — 1 (n-
bit encoding).

The images we will be considering will be "in


levels of gray". MATLAB® has a default palette that
can be activated using the colormap( 'gray')
instruction.
Type colormap( 'gray') then colormap. You get a (64
x 3) array with three identical columns of values
between 0 and 1:
>> colormap('gray')
>> colormap
ans =
0 0 0
0.0159 0.0159 0.0159
0.0317 0.0317 0.0317
0.0476 0.0476 0.0476
……… ……… ………..
0.9841 0.9841 0.9841
1.0000 1.0000 1.0000
Connection between the image array and the palette
Comments:
— In example 1 the zero values of the image1
array are redefined as 1 and therefore index
the color (0, 0, 0), which is black (Figure 6.2).
— Help for the commands image, imagesc and
colormap should particularly be looked into.
— The standard palette is constructed linearly.
Each column is of the type [0:1/63:1] ' (1/63 ~
0.0159). This does not quite correspond to the
perception we have of brightness. The visual
response is roughly proportional to the
logarithm of the intensity (Fechner-Weber law),
hence the progression of the levels of gray
should correspond to this law. In practice, the
palette's linear conformation makes our work
much easier since palette index lines and gray
levels are related by an affine relation.
— Other palettes come standard in the basic
version of MATLAB® to make the user's work
easier. Use the >>help color command to
learn more about them. Also, nothing stops
you from defining your own palettes. For
example, to get a display with 256 levels of
gray, all you need to do is create a cmap array
as follows:
>> cmap=[0:255]'*ones(1,3)/255;
>> colormap (cmap) ;
Importing images
If you don't have an image you can perform
tests on in MATLAB , you can always create one
based on raw format images (no header) using
image processing software. At the same time,
you can save the palette, if that is possible. The
following function allows you to read and/or
create a file that can be used directly by
MATLAB .
The image that was chosen is an image
universally used by "image processors" to
compare results obtained for different
implementations. It is referred to as Lena. We
assume that the data is stored as unsigned 8-bit
coded integers.
function pixc=raw2matf(NomFE,Nlig,Ncol,Tr,Fc,NomFS)
%========================================================
% Reading a raw image file %
% SYNOPSIS: pixc=RAW2MATF(NomFE,Nlig,Ncol,Tr,Fc,NomFS) %
% NomFE = raw file ([.raw]) %
% Nlig,Ncol = Image dimensions %
% Tr = when 'T': transposing the image %
% Fc = when 'F': creating the file NomFS (.mat) %
% NomFS = Resulting file ([.mat]) %
%========================================================
if nargin<6, NomFS='fictrav.mat'; end
if nargin<5, Fc='N'; end
if nargin<4, Tr='N'; end
%===== Raw image
nFS=f indstr(NomFE,'.');
if isempty(nFS),
NFE=[NomFE,'.raw'] ;
else
NFE=NomFE; NomFE=NomFE(l:nFS-l);
end
fid=fopen(NFE,'r'); [pixc,Npix]=fread(fid,'uchar');
if (Npix ~= Nlig*Ncol)
sprintf ('Dimensions error: %d*%d ~= %d',Nlig,Ncol,Npix)
return
end
pixc=reshape(pixc,Nlig,Ncol);
if Tr=='T',
pixc=pixc';
end
fclose(fid);
% =====Creating the .MAT file
if Fc=='F’
sprintf ('Creating the file %s',NomFS)
eval(['save ' NomFS ' pixc'])
end
return
The image can be loaded and displayed (Fig.4)
by the following program:
%===== TSTRAW2MAT.M
pixc=raw2matf('lena50',256,256,'T');
%===== Palette construction
cmap=([255:-l:0]'/255)*[l 1 1];
%===== Displaying with the new palette
imagesc(pixc) ;
colormap(cmap) ;
axis(' image')

In this program, the palette is defined, but it can also


be saved in the image processing application and
stored in the .mat file.
Comments:
— Recent versions of MATLAB allow you to
directly load and save images in formats such
as "bmp" (bit map), "tiff", "jpeg", "pcx", etc.
using the imread and imwrite functions.
— Notice that when a palette is used, the image
function works with an array of integer values
(the non-integer values are rounded) between
1 and M. The values above M are constrained
to M, and those below 1 are constrained to 1.
Type at the end of the previous program:
— Notice that when a palette is used, the image
function works with an array of integer values (the
non-integer values are rounded) between 1 and M.
The values above M are constrained to M, and
those below 1 are constrained to 1. Type at the end
of the previous program:
>> p256=pixc+256; subplot (121); image(p256); axis('image') ;
p0=pixc-256; subplot(122); image(p0); axis('image');
colormap(cmap);
You should see a white square and a black square.
- It is usually preferable to use the imagesc
function (suffix sc as in scale) which displays a
version with the same scale as the original
image: the values are changed to fit between 1
and size(colormap,1).
- The image's color levels can have values such
that it becomes difficult to display the image
because of a few extreme values. The use of
image or imagesc may not be satisfactory. The
following function allows you to improve the
display by modifying the color distribution:
function mydisp(pixr,cmap,stdpar,style)
% ==============================================
% Displaying with gray level control
% SYNOPSIS: MYDISP(pixr, cmap, stdpar, style)
% pixr = image
% cmap = palette
% stdpar = controls the min and max indices
% style = see AXIS function
%==============================================-
if nargin<2,
sprintf('Error on arguments');
return
end
if nargin<4, style='image'; end
if nargin<3, stdpar=3; end
if (stdpar <= 0 I stdpar >10) , stdpar=l; end
moy=mean(mean(pixr));stdp=stdpar*std(std(pir));
minp=moy-stdp; maxp=moy+stdp;
idx=l+(pixr-minp)*(size(cmap,l)-l)/(maxp-minp);
colormap(cmap); image(idx); axis(style)
return
- When using scanners or digital cameras, the
standard sampling values, in "dots per inch" (dpi),
are (300 x 600)1, (600 x 1,200), (1,600 x 3,200),
(2,700 x 2,700)... , and for quantification, 8, 10,
12... bits for each of the primary colors.
Arithmetical and logical operations
Because images in MATLAB® are matrices, the
usual operations can be directly applied to
them. In particular, arithmetic and logical
operations between images, pixel by pixel, can
be performed from the array values they are
associated with.
Thus, the sum of two images pixl and pix2 of the
same size can be written pixl + pix2, or just as
the square root of pix can be written sqrt(pix).
You only have to make sure that the obtained
values are consistent with the color palette, or
you can use the functions imagesc or mydisp.

As for the logical operations applied to the 8 bits


of the image pixels' binary representation, the
problem is trickier, because MATLAB® has no
integer type to which we could directly apply
the boolean operations (this was however
modified in the recent versions). The operations
have to be performed by extracting bits one by
one from the image matrices.
Here is an example: consider the two images in
Figure 5 - we are going to perform the AND
function between the figure on the left and the
figure on the right.
function pixr=ANDlog(pix1,pix2,L)
%==========================================
% Logical AND between two images
% SYNOPSIS: pixr=ANDL0G(pixl,pix2,L)
% pix1 = first image (gray palette)
% pix2 = second image (gray palette)
% L = number of bits for color coding
% pixr = image result
%========================================
if (nargin<3) , L=8; end
N1=size(pix1);
if (N1 ~= size(pix2)),
error('Matrix dimensions are not appropriate')
end
pixr=zeros(Nl);
%===== Extraction of the bits one by one
for k=1:L
pixr=pixr+ (rem(pix1,2) & rem(pix2,2)) * 2^(k-l);
pix1 = fix(pix1/2); pix2 = fix(pix2/2);
end
return
The program testlogic.m which uses the ANDlog fonction,
leads to Fig.6:

%===== TESTLOGIC.M
load lena25; % Loading and displaying
subplot (131); imagesc(pixc+1); % the first image
colormap(cmap); axis('image');
load testlog1; % Loading and displaying
subplot(132); imagesc(pixt1+1); % the second image
axis('image')
%===== Logical operation
pixr = ANDlog(pixc,pixt1,size(cmap,1));
subplot (133) ; imagesc (pixr+1) ; axis (' image ') ;

You might also like