background image

Image processing 4 

 

 

Example 1 
Image histogram and the appropriate threshold choice 
Basing on the histogram characteristics, two different thresholds were chosen (120 
and 180). Please, assess the results. 

 
L1=imread(

'portret.jpg'

figure 
imshow(L1) 
set(gcf,

'Color'

,([1 1 1]));figure; 

grid; 
imhist(L1); 
ylabel(

'Ilość pikseli'

'FontSize'

,15,

'FontName'

,

'Arial CE'

L2a=L1>120; 
figure; 
imshow(L2a); 
L2b=L1>180; 
figure; 
imshow(L2b) 

 
Example 2 
Threshold calculation using Otsu algorithm 
 

L1=imread(

'portret.jpg'

figure 
imshow(L1) 
set(gcf,

'Color'

,([1 1 1]));figure; 

grid; 
imhist(L1); 
ylabel(

'Pixels number

'FontSize'

,15,

'FontName'

,

'Arial CE'

level = graythresh(L1); 
L2 = im2bw(L1,level); 
imshow(L2) 

 
Exercise 1 
Take any image and, after its histogram analyses, try to choose the best threshold. Is 
this possible for every image? 
Then, use Otsu algorithm.  

 

Exercise 2 
Basing  on  image  „cells.bmp”  histogram  observation,  try  to  choose  two  thresholds 
(below and above) in order to show cells contours. This exercise should be done trial-
and-error method. 
  

background image

Example 3 
Two grayscale images (with the same dimensions) adding.  
In the first resulting image brightness is normalized, the second one – is without 
normalization.  
 

[L1a,map1a] = imread(

'gory.bmp'

); 

L1a=double(ind2gray(L1a,map1a))/255; 
figure; imshow(L1a); 
[L1b,map1b] = imread(

'drzewo.bmp'

); 

L1b=double(ind2gray(L1b,map1b))/255; 
figure;imshow(L1b) 
L2a=L1a+L1b; 
figure; imshow(mat2gray(L2a)) 
figure; imshow(uint8(round(L2a*255))) 

 
Example 4 
Two images adding that have different dimensions 
 

[L1a,map1a] = imread(

'Beaux.bmp'

); 

L1a=ind2gray(L1a,map1a); 
figure; imshow(L1a); 
L1a=L1a(201:650,101:700); % appropriate part cutting 
figure; imshow(L1a); 
[L1b,map1b] = imread(

'drzewo.bmp'

); 

L1b=ind2gray(L1b,map1b); 
figure;imshow(L1b) 
L2a=L1a+L1b; 
figure; imshow(mat2gray(L2a)) 
 

Caution: 
If the images are of different dimensions, it is necessary to cut the appropriate 
fragment from the bigger one. In the other case, the operation cannot be done, 
because matrix dimensions must agree.  

 

Example 5 

Method 1 
Two images mixing with different coefficients
  
 

[L1a,map1a] = imread(

'Beaux.bmp'

); 

L1a=ind2gray(L1a,map1a); 
L1a=L1a(201:650,101:700); 
figure; imshow(L1a); 
[L1b,map1b] = imread(

'drzewo.bmp'

); 

L1b=ind2gray(L1b,map1b); 
figure;imshow(L1b) 
L2a=imlincomb(0.3,L1a,0.7,L1b); % coefficients total has to be equal 1  
figure; imshow(L2a) 

 

background image

Method 2 

Two images mixing with different coefficients (changing every 2 seconds)  

 
[L1a,map1a] = imread(

'Beaux.bmp'

); 

L1a=ind2gray(L1a,map1a); 
L1a=L1a(201:650,101:700); 
figure; imshow(L1a); 
[L1b,map1b] = imread(

'drzewo.bmp'

); 

L1b=ind2gray(L1b,map1b); 
figure;imshow(L1b); 
figure 

for 

i=0:0.1:1 

L2=(double(L1a)*i+double(L1b)*(1-i))/255; 
imshow(L2); title([

'L1a'

,mat2str(i*100),

'%'

,

' + L1b'

,mat2str((1- 

i)*100),

'%'

]) 

pause(2) 

end 

 

Example 6 
"Imadd" function adds the constant value to the image or adds two images. 
Adding constant to every pixel   
 

L1 = imread(

'portret.jpg'

); 

figure; imshow(L1) 
L2 = imadd(L1,80); 
figure; imshow(L2) 

 
Exercise 3 
Add any two images using “imadd” function. Be careful, regarding matrices 
dimensions.  

 

Example 7 
Grayscale images “subtraction” (a hidden image)
  
 

[L1a,map1a] = imread(

'Beaux.bmp'

); 

L1a=ind2gray(L1a,map1a); 
L1a=L1a(201:650,101:700)>128; 
figure; imshow(L1a); 
[L1b,map1b] = imread(

'drzewo.bmp'

); 

L1b=ind2gray(L1b,map1b); 
figure;imshow(L1b); 
L1c=0.05*double(L1a)/255+0.95*double(L1b)/255; 
figure; imshow(L1c); 
L2=mat2gray(L1c-0.95*double(L1b)/255) 
figure; imshow(L2) 

 
Exercise 4 
Please, conduct image subtraction using “imabsdiff” function. Change the 
images sequence and see if there is any difference in the resulting image. 
 

background image

Exercise 5 
Now  use  “imsubtract”  function  for  the  same  operation.  Has  the  image 
sequence any consequence in the resulting image? 

 

Example 8 
Image multiplication 
Multiplication and dividing of different images can be used in the noise reduction.  
In landscape photography distant objects lack in sharpness, due to the fog that is 
visible in the blue colour channel. Channels multiplication (red and green) using 
“immultiply” allows to distinct greenery.   

 
L1=imread(

'gory.jpg'

figure;imshow(L1) 
L1r=double(L1(:,:,1))/255 
figure;imshow(L1r) 
L1g=double(L1(:,:,2))/255 
figure;imshow(L1g) 
L1b=double(L1(:,:,3))/255 
figure;imshow(L1b) 
L2=immultiply(L1g,L1r) 
figure;imshow(L2,[]) 
 

Exercise 6 
Divide the green channel by the red one using "imdivide". 
 
Example 9 
Minimum and maximum (from two images) calculation for every pixel at the 
same location   
 
L1=imread(

'gory.jpg'

figure;imshow(L1) 
L1r=double(L1(:,:,1))/255 
figure;imshow(L1r) 
L1g=double(L1(:,:,2))/255 
figure;imshow(L1g) 
L1b=double(L1(:,:,3))/255 
figure;imshow(L1b) 
L2a=max(L1g,L1b) 
L2b=min(L1g,L1b) 
figure 
imshow(L2a) 
figure 
imshow(L2b) 
 

background image

Example 10 
Method 1 
Logical operation NOT for the binary and grayscale image  

 
[L1a,map1a] = imread(

'Beaux.bmp'

); 

L1=ind2gray(L1a,map1a); 
L1a=L1>128; 
figure; imshow(L1a); 
L2a=~(L1a); 
figure;imshow(L2a); 
figure;imshow(L1); 
figure;imshow(bitcmp(L1,8)); 

 
Method 2 
Image completion obtained by using "imcomplement" function 
 

[L1a,map1a] = imread(

'Beaux.bmp'

); 

L1=ind2gray(L1a,map1a); 
figure; imshow(L1); 
L2=imcomplement(L1) 
figure;imshow(L2); 
 

Method 3 
An identical resulting image obtained by colour palette “grayscale” reversion   
 

[L1a,map1a] = imread(

'Beaux.bmp'

); 

L1=ind2gray(L1a,map1a); 
figure(

'Color'

'w'

); imshow(L1); 

colormap(gray(256)); 
colorbar; 
figure(

'Color'

'w'

); imshow(L1); 

colormap(flipud(gray(256))); 
colorbar; 
 

Example 11 
Method 1 
Binary images conjunction  
 

L1a=imread(

'wykr_10.tif'

figure;imshow(L1a) 
L1b=imread(

'wykr_14.tif'

figure;imshow(L1b) 
L2=L1a&L1b 
figure; imshow(L2) 
 

background image

Method 2 
 

L1a=imread(

'wykr_10.tif'

figure;imshow(L1a) 
L1b=imread(

'wykr_14.tif'

figure;imshow(L1b) 
L2=bitand(L1a,L1b) 
figure; imshow(L2) 

 
Exercise 7 
Basing on the example above, try to conduct OR, XOR and SUB operations.  
 
Example 12 
Grayscale images conjunction 
  

L1=imread(

'gory.jpg'

figure;imshow(L1) 
L1r=L1(:,:,1) 
figure;imshow(L1r) 
L1g=L1(:,:,2) 
figure;imshow(L1g) 
L1b=L1(:,:,3) 
figure;imshow(L1b) 
L2=bitand(L1r,L1g) 
figure; imshow(L2) 

 
Example 13 
Image projection on the surface of cylinder  
 

[x,y,z] = cylinder; 
L=imread(

'gory.jpg'

); 

warp(x,y,-z,L); 

 
... or on the solid that looks like a vase  
 

t=2/3:1/6:2*pi; 
y=sin(t)+3 
[x,y,z] = cylinder(y); 
L=imread(

'gory.jpg'

); 

warp(x,y,-z,L); 

 
Remove “-“ sign for “z” coordinate and see the difference.