Image processing 6
Example 1
Problems with pixels that lay at the image edge
Compare the results with the Exercise 1 from the last classes, drawing attention to the
edges.
L=imread('portret.jpg');
imshow (L), title('source image);
h=ones(5)/25
%filter mask, division for the sum of factors used instead normalization
L1=imfilter(L,h);
%image filtering
figure;
imshow(L1), title ('image without edge pixels replication');
L2=imfilter(L, h, 'replicate');
figure;
imshow (L2), title ('image with edge pixels replication');
Example 2
Special filters in Matlab
Image sharpening filter:
L = imread('portret.jpg');
L1 = imfilter(L,h);
h = fspecial('unsharp');
imshow(L);
figure, imshow(L1);
Exercise 1
In Matlab there are other predefined filters. Please, try to use ‘fspecial’ functions:
'average', 'gaussian', 'laplacian', 'log', 'motion', 'previt', 'sobel'. If the results are not
clear or you can’t say what masks were used, please ask the teacher for explanation.
Example 3
Median filter in use:
L=imread('portret.jpg');
figure(1);
imshow(L);
Mask=[5,5];
L1=medfilt2(L, Mask);
L1=mat2gray(L1);
figure(2); imshow(L1);
Exercise 2
Please, conduct image filtering with the low-pass average filter (mask size 5 x 5) and
save the image. Then, use median filter and compare the resulting images.
Exercise 3
Use median filter with the mask size of 3, 5, 15, 45 to filter the image ‘square.bmp’.
Observe the effect on the figure’s corners.
Example 4
Image filtering using minimal filter (artificial image):
L=ones([16, 16])*256;
L(6:11, 6:11)=(0:43:256)'*ones([1 6]);
L(14,14)=0;
L=uint8(L);
figure(1);
imshow(L, 'notruesize');
L1=imnoise(L, 'salt & pepper', 0.3);
figure(2);
imshow(L1,'notruesize');
Mask=[3,3]
L2=nlfilter(L1, Mask, 'min(x(:))');
L2=mat2gray(L2);
figure(3); imshow(L2,'notruesize');
Mask=[10,10];
L3=nlfilter(L1, Mask, 'min(x(:))');
L3=mat2gray(L3);
figure(4); imshow(L3, 'notruesize');
Exercise 4
Change the script in order to use maximum filter ‘max(x(:))’. Mask size should have the
size of 3x3 and 5x5.
Example 5
Image filtering using minimal filter (real image).
Caution: This operation can last a few minutes.
L=imread('Krakow_3.jpg','jpg');
L=rgb2gray(L);
L=uint8(L);
L=histeq(L);
figure(1);
imshow(L);
L1=imnoise(L, 'salt & pepper', 0.3);
% the last parameters describe the noise level
figure(2);
imshow(L1);
Mask=[3,3]
L2=nlfilter(L1, Mask, 'max(x(:))');
L2=mat2gray(L1);
figure(3); imshow(L2);
Mask=[2,2];
L3=nlfilter(L1, Mask, 'max(x(:))');
L3=mat2gray(L3);
figure(4); imshow(L3);
Exercise 5
Change the script in order to use maximum filter ‘max(x(:))’. Mask size should have the
size of 3x3 and 5x5.
Exercise 6
Conduct image binarization for ‘bacteria.bmp’ with the threshold equal 128, then apply
minimum and maximum filters. The difference between the images obtained can show
bacteria’s borders.
Example 6
Image filtration with 2D adaptive Wienner filter:
L=imread('Krakow_3.jpg','jpg');
L=rgb2gray(L);
L=uint8(L);
L=histeq(L);
figure(1);
imshow(L);
L1=imnoise(L, 'salt & pepper', 0.1);
figure(2);
imshow(L1);
L2=wiener2(L1, [5 5]);
figure(3);
imshow(L2);
Exercise 7
For the same image use the low-pass average filter (with the same mask size) and compare
the results.