This article is the "Digital image processing principle and practice (MATLAB version)" A Book of Code series PART3 (p81~135), code execution results please refer to the original book map, it is recommended to download the code before reading:
About the principle and practice of Digital Image processing (MATLAB version) of the book Code published instructions
http://blog.csdn.net/baimafujinji/article/details/40987807
P92
i = imread (' hepburn.jpg ');
% Note W and H1 these two templates are equivalent
w = [1 1 1;1 1 1;1 1 1]/9;
H1 = fspecial (' average ', [3 3]);
H2 = fspecial (' average ', [5 5]);
H3 = fspecial (' average ', [7 7]);
% perform simple smoothing of images
G1 = IMFilter (i, W, ' conv ', ' replicate ');
G2 = IMFilter (i, H2, ' conv ', ' replicate ');
G3 = IMFilter (i, H3, ' conv ', ' replicate ');
P98
i = imread (' baboon.jpg ');
h = fspecial (' Gaussian ', 7, 2);
g = IMFilter (i, H, ' conv ');
Subplot (121), imshow (i), title (' original image ');
Subplot (122), imshow (g), title (' Gaussian smooth ');
P103
i = Rgb2gray (Imread (' lena.jpg '));
I_noise = Imnoise (i, ' salt & Pepper ');
W1 = [1 2 1; 2 4 2; 1 2 1]/16;
OUTPUT1 = IMFilter (i_noise, W1, ' conv ', ' replicate ');
W2 = [1 1 1; 1 1 1; 1 1 1]/9;
Output2 = IMFilter (i_noise, W2, ' conv ', ' replicate ');
OUTPUT3 = MEDFILT2 (I_noise, [3, 3]);
P106
function B = Bfilter2 (A,w,sigma)
% apply different processing functions for grayscale image or color image selection
If size (a,3) = = 1
B = Bfltgray (A,w,sigma (1), Sigma (2));
Else
B = Bfltcolor (A,w,sigma (1), Sigma (2));
End
% function of bilateral filtering for grayscale images
function B = Bfltgray (a,w,sigma_d,sigma_r)
% Compute Gaussian template
[x, Y] = Meshgrid (-w:w,-w:w);
G = exp (-(x.^2+y.^2)/(2*sigma_d^2));
% for bilateral filtering
Dim = Size (A);
B = Zeros (Dim);
For i = 1:dim (1)
for j = 1:dim (2)
% extracts a local area, which corresponds to the size of the range nucleus
Imin = max (i-w,1);
IMax = Min (I+w,dim (1));
Jmin = max (j-w,1);
Jmax = Min (J+w,dim (2));
I = A (Imin:imax,jmin:jmax);
% compute the value of the domain core, which is the weight of the gray values template
H = exp (-(I-A (I,J)). ^2/(2*sigma_r^2));
% computed Bilateral filter response
F = H.*g ((Imin:imax)-i+w+1, (Jmin:jmax)-j+w+1);
B (I,J) = SUM (f (:). *i (:))/sum (f (:));
End
End
% function for bilateral filtering of color images
function B = Bfltcolor (a,w,sigma_d,sigma_r)
% converts the input RGB image into the CIE color space
If exist (' applycform ', ' file ')
A = Applycform (A,makecform (' Srgb2lab '));
Else
A = ColorSpace (' Lab<-rgb ', a);
End
[x, Y] = Meshgrid (-w:w,-w:w);
G = exp (-(x.^2+y.^2)/(2*sigma_d^2));
Sigma_r = 100*sigma_r;
% for filter processing
Dim = Size (A);
B = Zeros (Dim);
For i = 1:dim (1)
for j = 1:dim (2)
Imin = max (i-w,1);
IMax = Min (I+w,dim (1));
Jmin = max (j-w,1);
Jmax = Min (J+w,dim (2));
I = A (Imin:imax,jmin:jmax,:);
DL = I (:,:, 1)-A (i,j,1);
da = I (:,:, 2)-A (i,j,2);
db = I (:,:, 3)-A (i,j,3);
H = exp (-(dl.^2+da.^2+db.^2)/(2*sigma_r^2));
F = H.*g ((Imin:imax)-i+w+1, (Jmin:jmax)-j+w+1);
Norm_f = SUM (F (:));
B (i,j,1) = SUM (SUM (f.*i (:, 1)))/norm_f;
B (i,j,2) = SUM (SUM (f.*i (:, 2)))/norm_f;
B (i,j,3) = SUM (SUM (F.*i (:, 3)))/norm_f;
End
End
% Convert filter results back to RGB color space
If exist (' applycform ', ' file ')
B = Applycform (B,makecform (' Lab2srgb '));
Else
b = colorspace (' Rgb<-lab ', b);
End
P108
I = Imread (' cat.gif ');
i = double (i)/255;
W = 5;
Sigma = [3 0.1];
B = Bfilter2 (I,w,sigma);
P111
I = Imread (' cameraman.tif ');
H = fspecial (' unsharp ');
sharpened = IMFilter (i,h, ' replicate ');
Subplot (121), Imshow (I), title (' Original Image ')
Subplot (122), imshow (sharpened); Title (' Sharpened Image ')
P112
I = Imread (' cameraman.tif ');
Laplace=[0-1 0;-1 4-1; 0-1 0];
Data = double (I);
Laplaceimage=conv2 (data,laplace, ' same ');
% above this sentence can also be written in the following form, the effect is equivalent
%laplaceimage=imfilter (Data,laplace, ' conv ', ' same ');
Subplot (1,2,1); Imshow (Uint8 (laplaceimage)); Title (' Laplace image ');
% original image and Laplace image overlay
Datalap=imadd (Data,laplaceimage);
Subplot (1,2,2), Imshow (Uint8 (Datalap));
Title (' Sharpen enhanced image ');
P124
I = Imread (' fruits.jpg ');
SE = Strel (' Rectangle ', [10 10]);
I2 = Imerode (I, SE);
Figure (2), Imshow (I2)
P128
I = Imread (' fruits.jpg ');
SE = Strel (' Rectangle ', [10 10]);
I3 = Imdilate (I, SE);
Figure (3), Imshow (I3)
P133
I = Imread (' character.jpg ');
Figure, Imshow (I);
SE = Strel (' square ', 3);
Ie = Imerode (I, SE);
I2 = I-ie; % calculation Inner boundary
Figure (2), imshow (I2);
Id = Imdilate (I, SE);
I3 = Id-i; % COMPUTE outer boundary
Figure (3), imshow (I3);
P134
I = Imread (' character.jpg ');
SE = Strel (' square ', 3);
Ie = Imerode (I, SE);
Iee = Imerode (Ie, SE);
Id = Imdilate (I, SE);
IDD = Imdilate (Id, SE);
I1 = IE-IEE;
I2 = Idd-id;
I3 = I1 + I2;
Figure (3), imshow (I3);
(The code release is not finished, please wait for follow-up ...) )
"Digital image processing principle and practice (MATLAB version)" A book Code PART3