0% found this document useful (0 votes)
17 views

Image Processing Lecture - Q

Image processing lecture

Uploaded by

Maryam Eissa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Image Processing Lecture - Q

Image processing lecture

Uploaded by

Maryam Eissa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 93

Introduction to image processing

and MATLAB
What is image processing?

Image processing: involves changing the nature of an image in order to


either.

1. improve its pictorial information for human interpretation,


2. render it more suitable for autonomous machine perception.

We shall be concerned with digital image processing, which involves


using a computer to change the nature of a digital image.
A digital image can be considered as a large array of discrete dots, each
of which has a brightness associated with it. These dots are called
picture elements, or more simply pixels. The pixels surrounding a given
pixel constitute its neighbourhood.

Pixels, with a neighbourhood


Image processing

❑ Enhancing the edges of an image to make it appear sharper.

Image sharpening

❑ Removing “noise” from an image; noise being random errors in the


image.
Removing noise from an image

❑ Removing motion blur from an image.

Image deblurring
❑ Obtaining the edges of an image(edge detection).

Finding edges in an image


Some applications
Image processing has an enormous range of applications:
1. Medicine
• Inspection and interpretation of images obtained from X-rays,
MRI or CT scans.
• analysis of cell images, of chromosome karyotypes.
2. Agriculture
• inspection of fruit and vegetables—distinguishing good and
fresh product from old.
3 . Industry
• Automatic inspection of items on a production line,
4. Law enforcement
• Fingerprint analysis
• Sharpening or de-blurring of speed-camera images.
MATLAB and images

• The help in MATLAB is very good, use it!

• An image in MATLAB is treated as a matrix

• Every pixel is a matrix element

• All the operators in MATLAB defined on matrices can be used on


images:

+, - , * , / , ^ , sqrt , sin , cos , ….. etc.


Images in MATLAB

• MATLAB can import/export • Data types in MATLAB


several image formats – Double (64-bit double-precision floating
– BMP (Microsoft Windows Bitmap)
point)
– GIF (Graphics Interchange Files)
– Single (32-bit single-precision floating
– HDF (Hierarchical Data Format) point)
– JPEG-JPG (Joint Photographic – Int8 (8-bit signed integer)
Experts Group)
– Int16 (16-bit signed integer)
– PNG (Portable Network Graphics)
– Int32 (32-bit signed integer)
– TIFF (Tagged Image File format)
– Uint8 (8-bit unsigned integer)

– Uint16 (16-bit unsigned integer)

– Uint32 (32-bit unsigned integer)


Images in MATLAB
Types of digital images
• Binary images : {0,1}, Each pixel is just black or white. Since there are only
two possible values for each pixel, we only need one bit per pixel.

• Grayscale(Intensity )images : [0,1] or uint8, double etc. Each pixel is a shade


of grey, normally from 0 (black) to 255 (white), this range means that each
pixel can be represented by eight bits, or exactly one byte

• RGB (color)images : m-by-n-by-3(0 to 255 or 1to 256)

• Indexed images : m-by-3 color map


Image import and export
• Read and write images in Matlab
>> I=imread('cells.jpg');
>> imshow(I)
>> size(I)
ans = 479 600 3 (RGB image)
>> Igrey=rgb2gray(I);
>> imshow(Igrey)
>> imwrite(lgrey, 'cell_gray.tif', 'tiff’)
imwrite() is a powerful function in
Matlab that allows to save images to disk
in various formats. It offers flexibility and
control over image storage, making it a
valuable tool for image processing and analysis
tasks
imwrite(A, filename, format)
Images and Matrices
• How to build a matrix (or image)?
>> A = [ 1 2 3; 4 5 6; 7 8 9 ];
A= 1 2 3
4 5 6
7 8 9
>> B = zeros(3,3)
B= 0 0 0
0 0 0
0 0 0
>> C = ones(3,3)
C= 1 1 1
1 1 1
1 1 1

>>imshow(A) (imshow(A,[]) to get automatic pixel range)


Images and Matrices
• Accesing image elements (row, column) X
>> A(2,1)
ans = 4
• : can be used to extract a whole column or
row Y
>> A(:,2)
ans =
2
5
8
• or a part of a column or row
>> A(1:2,2) A=
ans = 1 2 3
2 4 5 6
5 7 8 9
Image Arithmetic
• Arithmetic operations such as addition, subtraction, multiplication and
division can be applied to images in MATLAB
– +, -, *, / performs matrix operations
>> A+A
ans = 2 4 6
8 10 12
14 16 18
>> A*A
ans = 30 36 42
66 81 96
102 126 150

• To perform an elementwise operation use . (.*, ./, .*, .^ etc)


>> A.*A
ans = 1 4 9 A=
1 2 3
16 25 36
4 5 6
49 64 81
7 8 9
Convert Between Image Types
Spatial Resolution

Spatial resolution is the density of pixels over the image: the


greater the spatial resolution, the more pixels are used to display
the image.
imresize(x,1/2);
will halve the size of the image.
If we apply imresize to the result with the parameter 2 rather than 1/2,
all the pixels are repeated to produce an image with the same size as
the original, but with half the resolution in each direction:

The effective resolution of this new image is only 128×128. We can do all
this in one line:

X2=imresize(imresize(x,1/2),2);
By changing the parameters of imresize, we can change the effective
resolution of the image to smaller amounts:
Arithmetic operations

Ex:
adding or subtracting 128 from each pixel in the image.
when we add 128, all grey values of 127 or greater will be mapped to
255. And when we subtract 128, all grey values of 128 or less will be
mapped to 0.
Adding and subtracting a constant

>> b=imread(’blocks.tif’);
>> b1=uint8(double(b)+128);
>> b1=imadd(b,128);
>> b2=imsubtract(b,128);
>> imshow(b1),figure,imshow(b2)
Arithmetic operations on an image: adding or subtracting a
constant
Using multiplication and division

y= x/2 b3=immultiply(b,0.5); or b3=imdivide(b,2)


y=2x b4=immultiply(b,2);
y=x/2+128 b5=imadd(immultiply(b,0.5),128); or
b5=imadd(imdivide(b,2),128);
Arithmetic operations on an image: multiplication and division
Histograms

Histogram is a graph indicating the number of times each grey level occurs in
the image, it consists of the histogram of image grey levels.
The image pout.tif and its histogram

to enhance its contrast, by spreading out its histogram.


There are two ways:
• Histogram stretching
• Histogram equalization
Histogram stretching (Contrast stretching)
Suppose we have an image:

A histogram of a poorly contrasted image, and a stretching function


We can stretch the grey levels 5-9 to grey levels 2-14 according to the
equation: (piecewise linear function)

where i is the original grey level and j its result after the transformation
Histogram equalization
Let :

L is the different grey levels 0,1,2,….. L -1, and the gray level is i
ni times in the image.( ‫)عدد مرات تكرارالون‬
n the total number of pixels in the image

To transform the grey levels to obtain a better contrasted image, we


change grey level i to:
Suppose a 4-bit greyscale image
Histogram indicating poor contrast and after equalization

>> p=imread(’pout.tif’);
>> ph=histeq(p);
>> imshow(ph),figure,imhist(ph),axis tight
The histogram after equalization
The index image
and its histogram

The image
equalized and its
histogram
Thresholding
Single thresholding
• A greyscale image is turned into a binary (black and white) image by first
choosing a grey level T in the original image, and then turning every pixel
black or white according to whether its grey value is greater than or less than T

• Thresholding is the simplest method of images segmentation


• Thresholding in Matlab:
Thresholded image of rice grains
Thresholding provides a very simple way of showing hidden aspects of an
image.

The paper image and result after thresholding


Double thresholding
The image spine.tif an the result after double thresholding
Applications of thresholding

1. When we want to remove unnecessary detail from an image.( like in the


rice and bacteria images)
2. To bring out hidden detail. This was illustrated with ( paper and spine
images).
3. When we want to remove a varying background from text or a drawing.
We can simulate a varying background by taking the image text.tif and
placing it on a random background.
Text on a varying background, and thresholding
We see that spatial filtering requires three steps:

1. position the mask over the current pixel,


2. form all products of filter elements with the corresponding
elements of the neighbourhood,
3. add up all the products.
This must be repeated for every pixel in the image.
Filtering in Matlab
The filter2 function does the job of linear filtering
for us; its use is
filter2(filter,image,shape)
Example: Look at the book
Frequencies
The frequencies of an image are the amount by
which grey values changes.
Frequencies; low and high pass filters

• High frequency components: are characterized


by large changes in grey values over small distances;
example of high frequency components are edges and
noise.

• Low frequency components: on the other hand,


are parts of the image characterized by little change in
the grey values. These may include backgrounds, skin
textures.
Frequencies; low and high pass filters

is a high pass filter.


High pass filtering

The images are shown in the figure.


(a) is the result of the Laplacian filter;
(b) shows the result of the Laplacian of Gaussian (“log”) filter.
Gaussian filters
Gaussian filters are a class of low-pass filters, all based on the
Gaussian probability distribution function
One dimensional gaussians

Two dimensional gaussians


Non-linear filters
The function to use in Matlab is nlfilter.
The nlfilter function requires three arguments: the image matrix,
the size of the filter, and the function to be applied.
Using non-linear filters
Noise
• Image noise is random variation of brightness or color
information in images, and is usually an aspect of electronic
noise.
• Noise is any degradation in the image signal, caused by external
disturbance. If an image is being sent electronically from one
place to another, via satellite or wireless transmission, or through
networked cable.
Types of noise

1. Salt and pepper noise


2. Gaussian noise
3. Speckle noise
4. Periodic noise
Salt and pepper noise
• Salt-and-pepper noise is a form of noise
sometimes seen on images.
• It is also known as impulse noise.
• This noise can be caused by sharp and sudden
disturbances in the image signal.
• It presents itself as sparsely occurring white and
black pixels (An image containing salt-and-pepper
noise will have dark pixels in bright regions and
bright pixels in dark regions)
Salt and pepper noise
• To add noise, we use the Matlab function imnoise
• To add salt and pepper noise:
Noise on an image
Gaussian noise
• Gaussian noise is an perfect form of white noise,
• Gaussian noise is white noise which is normally
distributed.
• If the image is represented as I and the Gaussian
noise by N, then
Speckle noise
• Gaussian noise can be modelled by random values
added to an image.
• speckle noise (or more simply just speckle) can be
modelled by random values multiplied by pixel
values.
• It is also called multiplicative noise
Periodic noise
• If the image signal is subject to a periodic,
• we might obtain an image corrupted by periodic
noise.

The twins image corrupted by periodic noise


Cleaning salt and pepper noise
Low pass filtering
• Given that pixels corrupted by salt and pepper noise
are high frequency components of an image,
• We should expect a low-pass filter should reduce
them (an average filter)
Cleaning salt and pepper noise
Low pass filtering
The effect is even more pronounced if we use a larger
averaging filter:
Attempting to clean salt & pepper noise with average filtering
Cleaning salt and pepper noise
Median filtering
• Median filtering seems almost tailor-made for removal of salt
and pepper noise.
• The median of a set is the middle value when they are sorted.
• A median filter is an example of a non-linear spatial filter.
• In Matlab, median filtering is implemented by the medfilt2
function:

Cleaning salt and pepper noise with


a median filter
Cleaning salt and pepper noise
Rank-order filtering
Median filtering is a special case of a more general process called
rank-order filtering.
Cleaning Gaussian noise
• It may sometimes happen that instead of just one image
corrupted with Gaussian noise. we have many different copies
of it.
• satellite imaging, we will obtain many different images of the
same place, in microscopy: we might take many different
images of the same object.
• Image averaging
Now we can take the average:
Image averaging to remove Gaussian noise
Average filtering:
If the Gaussian noise has mean 0, then we would expect that an
average filter would average the noise to 0. The larger the size of
the filter mask, the closer to zero.
Using averaging filtering to remove Gaussian noise
Wiener filtering:
• Filters which operate on this principle of least squares are called
Wiener filters.
• This filter is a (non-linear) spatial filter; we move a mask across
the noisy image, pixel by pixel.

where is the mean, is the variance in the mask,

• Matlab function wiener2 which implements Wiener


filtering
• We will use the wiener2 function, which can take an
optional parameter indicating the size of the mask to be
used.
The default size is . We shall create four images:
Examples of Wiener filtering to remove Gaussian noise
We can achieve very good results for noise where the variance is
not as high as that in our current image.

Using Wiener filtering to remove Gaussian noise with low variance

You might also like