Matlab – Traitement d’images

Histogramme
Calcul et affichage d’histogramme d’une image:

Image=imread('cameraman.tif');
[l,c]=size(Image);
H=zeros(256,1);
for i=1:l
   for j=1:c
	 H(Image(i,j)+1)= H(Image(i,j)+1)+1;    
    end 
  end
bar(H)

 


Avec la fonction prédefinie:

imhist(Image)

Egalisation d’histogramme
l’égalisation d’histogramme est une méthode d’ajustement du contraste d’une image numérique qui utilise l’histogramme. Elle consiste à appliquer une transformation sur chaque pixel de l’image, et donc d’obtenir une nouvelle image à partir d’une opération indépendante sur chacun des pixels. Cette transformation est construite à partir de l’histogramme cumulé de l’image de départ.

Étapes à suivre
1. Calcul de l’histogramme h(k) avec k Є[0; 255]
2. Histogramme cumulé
3. Transformation des niveaux de gris de l’image

En Matlab, la fonction prédéfinie histeq permet d’obtenir l’histogramme égalisé d’une image.

I = imread('pout.tif');
imshow(I)
imhist(I)
I2 = histeq(I); 
imshow(I2)
subplot ( 2 , 2 , 1 ) ; imshow( I) ; title ( 'Image originale' ) ;
subplot ( 2 , 2 , 3 ) ; imhist(I) ; title ( 'Histogramme original' ) ;
subplot ( 2 , 2 , 2 ) ; imshow(I2) ; title ( 'Image traitée (égalisation d''histogramme)' ) ;
subplot ( 2 , 2 , 4 ) ; imhist(I2) ; title ( 'Histogramme égalisé' ) ;

Ajout de bruit à une image:

% Read the test Image
mygrayimg = imread('cameraman.tif');
subplot(2,3,1); 
imshow(mygrayimg); 
title('Original Image');

% Add Salt and pepper noise with noise density 0.001
salt = imnoise(mygrayimg,'salt & pepper',0.001);
subplot(2,3,2);
imshow(salt);
title('Salt & Pepper Image (bruit impulsionnel faible)');

% Add Salt and pepper noise with noise density 0.03
salt = imnoise(mygrayimg,'salt & pepper',0.03);
subplot(2,3,3);
imshow(salt);
title('Salt & Pepper Image (bruit impulsionnel fort)');


% Add Gaussian noise with mean 0 and variance 0.003 
gau = imnoise(mygrayimg, 'gaussian', 0, 0.003);
subplot(2,3,4);
imshow(gau);
title('Gaussian Image- mean 0 and variance 0.003');

% Add Gaussian noise with mean 0 and variance 0.02 
gau = imnoise(mygrayimg, 'gaussian', 0, 0.02);
subplot(2,3,5);
imshow(gau);
title('Gaussian Image- mean 0 and variance 0.02');

% Original Image plus sinusoidal noise    
subplot(2,3,6);
[x y] = meshgrid(1:256,1:256);
mysinusoidalnoise = 15 * sin(2*pi/14*x+2*pi/14*y);
mynoiseimg1 = double(mygrayimg) + mysinusoidalnoise;
imshow(mynoiseimg1,[]);
title('Generated Sinusoidal noise');

Filtre Moyen:

org=imread('peppers.png');
m = imnoise(org, 'gaussian', 0, 0.03); % Ajout d'un bruit blanc Gaussien, de moyenne nulle
filterWindow3 = ones(3) / 9;
filteredImage3 = imfilter(m, filterWindow3);
filterWindow5 = ones(5) / 25;
filteredImage5 = imfilter(m, filterWindow5);
filterWindow9 = ones(9) / 81;
filteredImage9 = imfilter(m, filterWindow9);
filterWindow15 = ones(15) / 225;
filteredImage15 = imfilter(m, filterWindow15);
subplot ( 2 , 3 , 1 ) ; imshow(org) ; title ( 'Image originale' ) ;
subplot ( 2 , 3 , 2 ) ; imshow(m) ; title ( 'Image bruitée (bruit Gaussien)' ) ;
subplot ( 2 , 3 , 3 ) ; imshow(filteredImage3) ; title ( 'Image filtrée (Moyen 3x3)');
subplot ( 2 , 3 , 4 ) ; imshow(filteredImage5) ; title ( 'Image filtrée (Moyen 5x5)');
subplot ( 2 , 3 , 5 ) ; imshow(filteredImage9) ; title ( 'Image filtrée (Moyen 9x9)');
subplot ( 2 , 3 , 6 ) ; imshow(filteredImage15) ; title ( 'Image filtrée (Moyen 15x15)');

Filtre Gaussien:

I = imread('cameraman.tif');
Iblur = imgaussfilt(I,2); % Image filtrée avec un filtre Gaussiin- Sigma = 2
subplot ( 1 , 2 , 1 ) ; imshow(I) ; title ( 'Image originale');
subplot ( 1 , 2 , 2 ) ; imshow(Iblur) ; title ( 'image filtrée avec un filtre Gaussien');

Filtre médian:

I = imread('eight.tif');
J = imnoise(I,'salt & pepper',0.02);
K = medfilt2(J);
subplot ( 1 , 3 , 1 ) ; imshow(I) ; title ( 'Image originale');
subplot ( 1 , 3 , 2 ) ; imshow(J) ; title ( 'Image bruitée');
subplot ( 1 , 3 , 3 ) ; imshow(K) ; title ( 'image filtrée avec un filtre médian');

Détection de contour:

I = imread('coins.png');
BW1 = edge(I,'sobel');
BW2 = edge(I,'prewitt');
BW3 = edge(I,'canny');
subplot ( 2 , 2 , 1 ) ; imshow(I) ; title ( 'Image originale' ) ;
subplot ( 2 , 2 , 2 ) ; imshow(BW1) ; title ( 'Contour avec Sobel' ) ;
subplot ( 2 , 2 , 3 ) ; imshow(BW2) ; title ( 'Contour avec prewitt' ) ;
subplot ( 2 , 2 , 4 ) ; imshow(BW3) ; title ( 'Contour avec Canny' ) ;

Filtre morphologique (érosion):

a=[
0   0   0   1   0   0   0
0   1   1   1   1   1   0
0   1   0   1   1   1   0
1   1   1   1   1   1   0
0   0   1   1   1   1   0
0   1   1   1   1   1   0
0   0   0   0   0   1   0];
 
st1=[1 1 1 
     1 1 1
     1 1 1];
st2=[0 1 0 
     1 1 1
     0 1 0];
st3=[0 1 0 
     0 1 0
     0 1 0]; 
st4=[0 0 0 
     1 1 1
     0 0 0]; 
i1= imerode(a,st1);
 i2= imerode(a,st2);
 i3= imerode(a,st3);
 i4= imerode(a,st4);
 subplot (2,3,1), imshow(a), title('original');
subplot (2,3,2), imshow(i1),  title ('erosion cube');
subplot (2,3,3), imshow(i2),  title ('erosion cercle');
subplot (2,3,4), imshow(i3),  title ('erosion |');
subplot (2,3,5), imshow(i4),  title ('erosion ---');

Transformée de Fourier Rapide sur l’image:

[f, p]=uigetfile('*','selection de fichier');
S=imread(fullfile(p,f));
subplot(1,3,1); imshow (S);  title ( 'Image originale');
FS=fft2(double(S)); 
module=abs(fftshift(FS)); 
Max=max(max(max(abs(module))));
subplot(1,3,2), imshow(module *255 /Max);colormap gray ; title ( 'Spectre d''amplitude');
phase=angle(fftshift(FS)); 
subplot(1,3,3); imshow(phase,[-pi,pi]); colormap gray; title ( 'Spectre de phase');