Image Processing Basics in MATLAB
· You will first need to
transfer some image files to a folder in your MATLAB path.
· The imread( )
command let’s you read in most any image file format
and the image data can be stored in an array in your workspace. Try this…
close all; clear; clc
a=imread('andrew.jpg'); % read image and stores data in array
‘a’
size(a) % determine size
of array
class(a) % determine the
data format
· The size of the image is the
number of picture elements (pixels) vertically
and horizontally.
·
The class of the data is uint8. This stands for unsigned integer. Each pixel is stored with 8 bits. With 8 bits, we can have 256 unique numbers. Each of these numbers in the image array
represents the brightness of a pixel.
·
Look at the pixel data and you will see they range from 0 to 255:
a
· We will use the image( ) command to display our image
figure(1)
image(a) % displays array
a as image
axis image % makes the pixels square
· It won’t look right until we
tell MATLAB the proper “colormap” to be used. That is, we need to tell it what color
corresponds to each number (0-255). For
this image we want 255 to be white, 0 to be black and the rest shades of gray.
colormap(gray(256)) % set standard 8-bit grayscale colormap
· In order to treat our image
as a standard numerical array, we have to convert it to a double precision
floating-point array (64 bits per element).
ad=double(a);
· We can make our image a
“negative” by simply making the bright pixels dark and vice versa.
figure(1)
adneg=255-ad; % makes 255 (bright) pixels dark (0),
makes dark light
image(adneg)
colormap(gray(256))
axis image
· We can change the contrast
and brightness by linearly scaling each pixel value such that
output pixels = (input pixels) x
(gain factor) + (bias term).
The gain factor controls the
contrast. The bias term controls the
brightness.
adscale=2*ad-50;
figure(2)
subplot(211)
image(ad)
colormap(gray(256))
axis image
title('Original');
subplot(212)
image(adscale)
colormap(gray(256))
axis image
title('Contrast
Stretched');
· The above modifications are
called point operations because they operate on each pixel or point
independently. To do things like smooth
or sharpen an image, we need to perform spatial operations, where neighboring
pixels are involved in the modification of a given pixel.
· Image smoothing is
accomplished by averaging neighboring pixels.
Each output pixel is the average of a block of input pixels.
close all; clc
[sy,sx]=size(ad);
adout=ad;
for i=2:sy-1
for j=2:sx-1
adout(i,j)=(1/9)*(
ad(i,j+1)+ad(i-1,j+1)+ad(i+1,j+1)+ad(i,j)+ad(i-1,j+1)+ad(i+1,j+1)+ad(i,j-1)+ad(i-1,j-1)+ad(i+1,j-1) );
end
end
figure(1)
subplot(211)
image(ad)
colormap(gray(256))
axis image
title('Original ');
subplot(212)
image(adout)
colormap(gray(256))
axis image
title('Smoothed Image
')
· Sharpening is accomplished
by subtracting a given pixel from its neighbors. We then add this difference back to the original
pixel to accentuate the differences among neighboring pixels in the output
image.
[sy,sx]=size(ad);
adout=ad;
for i=2:sy-1
for j=2:sx-1
% current sample
minus the average if its neighboring pixels
delta=ad(i,j)-.25*(ad(i-1,j)+ad(i,j-1)+ad(i+1,j)+ad(i,j+1));
% add this “difference” to the current
sample for sharpening effect
adout(i,j)=ad(i,j)+2*delta;
end
end
figure(1)
subplot(211)
image(ad)
colormap(gray(256))
axis image
title('Original');
subplot(212)
image(adout)
colormap(gray(256))
axis image
title('Sharpened
Image')
· In the additive color system
(colors made by adding light), any color can be created by adding the
appropriate amount of three primary colors.
A color image is actually three images.
One containing the red intensity of each pixel, one containing the green
and one containing the blue. This is a
so-called “RGB” image. Let’s load and
display a color image into MATLAB.
clear; close all; clc
acolor=imread('andrewcol.jpg');
size(acolor)
class(acolor)
figure(1)
image(acolor) % default colormap works for a
standard color image.
· Note that the size of acolor is 384 x 512 x
3. This is a 3D array! The intensities of red, green and blue are
extracted as follows:
red=acolor(:,:,1);
green=acolor(:,:,2);
blue=acolor(:,:,3);
· Let’s look at the red, green
and blue component images…
blank=zeros(size(red)); % array of zeros
% Note: cat( ) àconcatenate
arrays in the dimension number listed
red2=cat(3,red,blank,blank); % color image with only red component
green2=cat(3,blank,green,blank);
% color image with only green
blue2=cat(3,blank,blank,blue); % color image with only blue
figure(2)
subplot(311)
image(red2);
axis image
subplot(312)
image(green2);
axis image
subplot(313)
image(blue2);
axis image
· Digital images are not just
about snapshots. Images can be a useful
way to visualize all kinds of two-dimensional data. Images formed from phenomena we can’t see
with our eyes are called non-visible images.
A spectrogram (from our sound processing exercise) is an example of a
non-visible image. Below are two more
examples…
Magnetic Resonance Image (MRI)
More information on this
type of imaging technique is available at:
http://www.mrispringfield.com/WHATIS.HTM

· Our eyes are sensitive to
light in the wavelength range .4e-6 to .7e-6 meters. Most of what we see is reflected light off
the objects around us. However, electromagnetic
radiation is emitted by all matter which is at a temperature above absolute
zero.
· Objects near room
temperature emit radiation at long wavelengths (e.g., 3e-6 to 12e-6
meters). We can build detectors that can
sense radiation at these wavelengths (infrared wavelengths) and the strength of
the detected radiation can be the basis for an image. At these wavelengths, we are essentially
“seeing” a signal proportional to the object’s temperature.
· Because such infrared
imagery is detecting primarily emitted (not reflected) radiation, these systems
can operate at night.
· Here is an image of a former
graduate student taken with an infrared camera.
Note that the soda can he is holding is cold and so are his fingertips
(from holding the can). The face is
warm, especially where blood vessels are close to the surface.

· Here is a picture of the
camera that took the infrared picture above.
Note it has a big cylindrical container for liquid nitrogen to cool the
camera so that it’s own heat
won’t mess up the picture. The lens is
on the front and the electronics are at the back.
