Code of the book "Digital Image Processing Principles and Practices (MATLAB)" Part6, matlab Digital Image Processing

Source: Internet
Author: User

Code of the book "Digital Image Processing Principles and Practices (MATLAB)" Part6, matlab Digital Image Processing

This article is part of the code series "Part6" in the book "Digital Image Processing Principles and Practices (MATLAB version)", which records the code from 281st to 374th pages for readers to download and use. For the code execution result, see the original book layout. We recommend that you read the following before downloading the Code:

Introduction to the code release in the book "Digital Image Processing Principles and Practices (MATLAB )"

Http://blog.csdn.net/baimafujinji/article/details/40987807

P338

I = double (imread ('vase. tif '));
[C, S] = wavedec2 (I, 2, 'db1 ');
A2 = appcoef2 (C, S, 'db1', 2 );
Dh1 = detcoef2 ('h', C, S, 1 );
Dv1 = detcoef2 ('V', C, S, 1 );
Dd1 = detcoef2 ('D', C, S, 1 );
Dh2 = detcoef2 ('h', C, S, 2 );
Dv2 = detcoef2 ('V', C, S, 2 );
Dd2 = detcoef2 ('D', C, S, 2 );
[X, y] = size (I );
Img = zeros (x, y );
Img (1: x/4, 1: y/4) = im2uint8 (mat2gray (a2 ));
Img (x/4) + 1): y/2, 1: y/4) = im2uint8 (mat2gray (dv2 ));
Img (x/4) + 1): x/2, 1: y/4) = im2uint8 (mat2gray (dv2 ));
Img (1: x/4, (y/4) + 1): y/2) = im2uint8 (mat2gray (dh2 ));
Img (x/4) + 1): x/2, (y/4) + 1): y/2) = im2uint8 (mat2gray (dd2 ));
Img (x/2) + 1): x, 1: y/2) = im2uint8 (mat2gray (dv1 ));
Img (1: x/2, (y/2) + 1): y) = im2uint8 (mat2gray (dh1 ));
Img (x/2) + 1): x, (y/2) + 1): y) = im2uint8 (mat2gray (dd1 ));
Imshow (img, []);

P341-1

X1 = imread('cathe1.bmp ');
X2 = imread('cathe2.bmp ');
XFUS = wfusimg (X1, X2, 'sym4 ', 5, 'mean', 'max ');
Imshow (XFUS, []);

P341-2

X1 = imread('cathe1.bmp ');
X2 = imread('cathe2.bmp ');
M1 = double (X1)/256;
M2 = double (X2)/256;
N = 4;
Wtype = 'sym4 ';
[C0, s0] = wavedec2 (M1, N, wtype );
[C1, s1] = wavedec2 (M2, N, wtype );
Length = size (c1 );
Coef_Fusion = zeros (1, length (2 ));
% Low frequency coefficient processing, average value
Coef_Fusion (1: s1 (1, 1) = (c0 (1: s1 (1, 1) + c1 (1: s1 (1, 1)/2;
% Process the high-frequency coefficient. If the absolute value is greater, matrix multiplication is used here.
MM1 = c0 (s1 (1, 1) + 1: length (2 ));
MM2 = c1 (s1 (1, 1) + 1: length (2 ));
Mm = (abs (MM1)> (abs (MM2 ));
Y = (mm. * MM1) + ((~ Mm). * MM2 );
Coef_Fusion (s1 (1, 1) + 1: length (2) = Y;
% Refactoring
Y = waverec2 (Coef_Fusion, s0, wtype );
Imshow (Y, []);

P344

I = imread('noise_lena.bmp ');
[Thr, sorh, keepapp] = ddencmp ('den ', 'wv', I );
De_ I = wdencmp ('gbl', I, 'sym4 ', 2, thr, sorh, keepapp );
Imwrite (im2uint8 (mat2gray (de_ I), 'denoise_lena.bmp ');

P361

Function diff_im = anisodiff (im, num_iter, delta_t, k, option)

Im = double (im );
% Assign Initial Value
Diff_im = im;

% Convolution template used to calculate the direction gradient
HN = [0 1 0; 0-1 0; 0 0 0];
HS = [0 0 0; 0-1 0; 0 1 0];
HE = [0 0 0; 0-1 1; 0 0 0];
HW = [0 0 0; 1-1 0; 0 0 0];
HNE = [0 0 1; 0-1 0; 0 0 0];
HSE = [0 0 0; 0-1 0; 0 0 1];
HSW = [0 0 0; 0-1 0; 1 0 0];
HNW = [1 0 0; 0-1 0; 0 0 0];

% Exclusive diffusion filtering
For t = 1: num_iter
% Calculate the gradient
NablaN = conv2 (diff_im, hN, 'same ');
NablaS = conv2 (diff_im, hS, 'same ');
NablaW = conv2 (diff_im, hW, 'same ');
NablaE = conv2 (diff_im, hE, 'same ');
NablaNE = conv2 (diff_im, hNE, 'same ');
NablaSE = conv2 (diff_im, hSE, 'same ');
NablaSW = conv2 (diff_im, hSW, 'same ');
NablaNW = conv2 (diff_im, hNW, 'same ');
% Calculate Diffusion Coefficient
% OPTION 1: c (x, y, t) = exp (-(nablaI/kappa). ^ 2)
If option = 1
CN = exp (-(nablaN/k). ^ 2 );
CS = exp (-(nablaS/k). ^ 2 );
CW = exp (-(nablaW/k). ^ 2 );
CE = exp (-(nablaE/k). ^ 2 );
CNE = exp (-(nablaNE/k). ^ 2 );
CSE = exp (-(nablaSE/k). ^ 2 );
CSW = exp (-(nablaSW/k). ^ 2 );
CNW = exp (-(nablaNW/k). ^ 2 );
% OPTION 2: c (x, y, t) = 1./(1 + (nablaI/kappa). ^ 2)
Elseif option = 2
CN = 1./(1 + (nablaN/k). ^ 2 );
CS = 1./(1 + (nablaS/k). ^ 2 );
CW = 1./(1 + (nablaW/k). ^ 2 );
CE = 1./(1 + (nablaE/k). ^ 2 );
CNE = 1./(1 + (nablaNE/k). ^ 2 );
CSE = 1./(1 + (nablaSE/k). ^ 2 );
CSW = 1./(1 + (nablaSW/k). ^ 2 );
CNW = 1./(1 + (nablaNW/k). ^ 2 );
End

% Calculate the result of one Iteration
Diff_im = diff_im + delta_t *(...
CN. * nablaN + cS. * nablaS + cW. * nablaW + cE. * nablaE +...
CNE. * nablaNE + cSE. * nablaSE + cSW. * nablaSW + cNW. * nablaNW );
End

P363

Num_iter = 50; delta_t = 0.125;
K = 4; option = 2;
I = imread('noise_lena.bmp ');
Diff = anisodiff (I, num_iter, delta_t, k, option );

P370

Function x = Thomas (N, alpha, beta, gama, d)

X = d;
M = zeros (1, N); l = zeros (1, N );
M (1) = alpha (1 );
For I = 2: N
L (I) = gama (I)/m (I-1 );
M (I) = alpha (I)-l (I) * beta (I-1 );
End
Y = zeros (1, N );
Y (1) = d (1 );
For I = 2: N
Y (I) = d (I)-l (I) * y (I-1 );
End

X = zeros (1, N );
X (N) = y (N)/m (N );
For I = N-1 :-
X (I) = (y (I)-beta (I) * x (I + 1)/m (I );
End

P371

Function Ig = gauss (I, ks, sigma2)

[Ny, Nx] = size (I );
Hks = (ks-1)/2; % half of Gaussian Kernel
%-One-dimensional convolution
If (Ny <ks)
X = (-hks: hks );
Flt = exp (-(x. ^ 2)/(2 * sigma2); % One-dimensional Gaussian function
Flt = flt/sum (flt); % Normalization

X0 = mean (I (:, 1: hks); xn = mean (I (:, Nx-hks + 1: Nx ));
EI = [x0 * ones (Ny, ks) I xn * ones (Ny, ks)];
Ig = conv (eI, flt );
Ig = Ig (:, ks + hks + 1: Nx + ks + hks );
Else
%-Two-dimensional convolution
X = ones (ks, 1) * (-hks: hks); y = x ';
Flt = exp (-(x. ^ 2 + y. ^ 2)/(2 * sigma2); % Two-Dimensional Gaussian function
Flt = flt/sum (flt); % Normalization

If (hks> 1)
XL = mean (I (:, 1: hks) '; xR = mean (I (:, Nx-hks + 1: Nx )')';
Else
XL = I (:, 1); xR = I (:, Nx );
End
EI = [xL * ones (1, hks) I xR * ones (1, hks)];
If (hks> 1)
XU = mean (eI (1: hks, :)); xD = mean (eI (Ny-hks + 1: Ny ,:));
Else
XU = eI (1, :); xD = eI (Ny ,:);
End
EI = [ones (hks, 1) * xU; eI; ones (hks, 1) * xD];
Ig = conv2 (eI, flt, 'valid ');
End

P372

Img = imread('Lena.bmp ');
Img = double (Img );

[Nrow, ncol] = size (Img );

N = max (nrow, ncol );
% Store the Three-diagonal matrix
Alpha = zeros (1, N); beta = zeros (1, N); gama = zeros (1, N );
% Store intermediate results
U1 = zeros ([nrow, ncol]);
U2 = zeros ([nrow, ncol]);
Timestep = 5;

% Used to control the number of iterations
% Iterations = 2;
% For times = 1: iterations
I _temp = gauss (Img, 3, 1 );
Ix = 0.5 * (I _temp (:, [2: ncol, ncol])-I _temp (:, [: ncol-1]);
Iy = 0.5 * (I _temp ([2: nrow, nrow], :)-I _temp ([: nrow-1], :));
K = 10
Grad = Ix. ^ 2 + Iy. ^ 2;
G = 1./(1 + grad/K * K); % edge oppression factor

% Solve u1 row by row using the Thomas algorithm
For I = 1: nrow
Beta (1) =-0.5 * timestep * (g (I, 2) + g (I, 1 ));
Alpha (1) = 1-beta (1 );
For j = 2: ncol-1
Beta (j) =-0.5 * timestep * (g (I, j + 1) + g (I, j ));
Gama (j) =-0.5 * timestep * (g (I, J-1) + g (I, j ));
Alpha (j) = 1-beta (j)-gama (j );
End
Gama (ncol) =-0.5 * timestep * (g (I, ncol) + g (I, ncol-1 ));
Alpha (ncol) = 1-gama (ncol );
U1 (I, :) = Thomas (ncol, alpha, beta, gama, Img (I ,:));
End

% Using the Thomas algorithm to solve u2 one by one
For j = 1: ncol
Beta (1) =-0.5 * timestep * (g (2, j) + g (1, j ));
Alpha (1) = 1-beta (1 );
For I = 2: nrow-1
Beta (j) =-0.5 * timestep * (g (I + 1, j) + g (I, j ));
Gama (j) =-0.5 * timestep * (g (I-1, j) + g (I, j ));
Alpha (j) = 1-beta (j)-gama (j );
End
Gama (nrow) =-0.5 * timestep * (g (nrow, j) + g (nrow-1, j ));
Alpha (nrow) = 1-gama (nrow );
U2 (:, j) = Thomas (nrow, alpha, beta, gama, Img (:, j ));
End
Img = 0.5*(u1 + u2 );
% Display processing result
Imshow (uint8 (Img ));
% End


(Code release is not complete. Please wait for the future ...)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.