"Digital image processing principle and practice (MATLAB version)" A book Code PART8

Source: Internet
Author: User

This article is the "Digital image processing principle and practice (MATLAB version)" A Book of code series PART8, the Book of the No. 375 to No. 415 page of the code, for the needs of readers to download research and use. At this point in the book code release is nearing the end, I hope that these sources can be useful to the readers. The results of the code execution are shown in the original book, and we recommend that you read the following before downloading the code:

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

P385-1

Function y = TV (X)
[M,n] = size (X);
Dh = diff (x,[],1);
Dh = [Dh;zeros (1,n)];
Dv = diff (x,[],2);
DV = [dv zeros (m,1)];
y = sum (sum (sqrt (dh.^2+dv.^2)));
End

P385-2

Function y = ATV (X)
[M,n] = size (X);
Dh =-diff (x,[],1);
Dh = [Dh;zeros (1,n)];
Dv =-diff (x,[],2);
DV = [dv zeros (m,1)];
y = SUM (SUM (ABS (DH) +abs (Dv)));
End

P387

I = Double (Rgb2gray (Imread (' lena.bmp ')));
I0 = I;
Ep=1; dt=0.25; lam=0;
ep2=ep^2; [Ny,nx]=size (I);
iter = 80;

For I=1:iter,
% Center difference method for gradient and differential calculation
% WN N EN
% W O E
% WS S ES
i_x = (I (:, [2:nx NX])-I (:, [1 1:nx-1]))/2; % Ix = (e-w)/2
I_y = (I ([2:ny NY],:)-I ([1 1:ny-1],:))/2;% Iy = (S-N)/2
I_xx = I (:, [2:nx NX]) +i (:, [1 1:nx-1]) -2*i; % Ixx = E+w-2*o
I_yy = I ([2:ny NY],:) +i ([1 1:ny-1],:) -2*i;% Iyy = S+n-2*o
Dp = I ([2:ny ny],[2:nx NX]) +i ([1 1:ny-1],[1 1:nx-1]);
Dm = I ([1 1:ny-1],[2:nx NX]) +i ([2:ny ny],[1 1:nx-1]);
I_xy = (DP-DM)/4; % Ixy = Iyx = ((es+wn)-(EN+WS))/4

Num = i_xx.* (ep2+i_y.^2) -2*i_x.*i_y.*i_xy+i_yy.* (ep2+i_x.^2);
Den = (ep2+i_x.^2+i_y.^2). ^ (3/2);
i_t = Num./den + lam.* (i0-i);
i=i+dt*i_t; Percent evolve image by DT
End
Imshow (i,[]);

P400

RGB = Imread (' moon.jpg ');
I = Rgb2gray (RGB);
J = Imnoise (I, ' Gaussian ', 0,0.025);
Imshow (J)
K = Wiener2 (j,[5 5]);
Figure, Imshow (K)

P401

I = im2double (Imread (' cameraman.tif '));
Imshow (I), title (' Original Image ');

% simulates motion blur, generating a point diffusion function psf with a corresponding linear motion length of 21 pixels and an angle of 11
LEN = 21;
THETA = 11;
PSF = fspecial (' Motion ', LEN, THETA);
Blurred = IMFilter (I, PSF, ' conv ', ' circular ');
Figure, Imshow (blurred), title (' Blurred Image ');

% restoration of motion blurred images
Result_w1= Deconvwnr (blurred, PSF);
Figure, Imshow (RESULT_W1), title (' Restoration of blurred Image ')

% Analog additive noise
Noise_mean = 0;
Noise_var = 0.0001;
Blurred_noisy = imnoise (blurred, ' Gaussian ', Noise_mean, Noise_var);
Figure, Imshow (Blurred_noisy), title (' Simulate Blur and Noise ')

% assumes no noise in case of image restoration
ESTIMATED_NSR = 0;
WNR2 = Deconvwnr (Blurred_noisy, PSF, ESTIMATED_NSR);
Figure, Imshow (WNR2)
Title (' Restoration of blurred, Noisy Image Using NSR = 0 ')

% uses a better signal-to-noise power ratio estimate to restore the image
ESTIMATED_NSR = Noise_var/var (I (:));
WNR3 = Deconvwnr (Blurred_noisy, PSF, ESTIMATED_NSR);
Figure, Imshow (WNR3)
Title (' Restoration of blurred, Noisy Image Using NSR ');

P404

% Load Original image
I = Imread (' board.tif ');
i = i (50+[1:256],2+[1:256],:);
Figure, Imshow (I)
Title (' Original Image ')

% blur processing
PSF = fspecial (' Gaussian ', 5,5);
Blurred = IMFilter (I,PSF, ' symmetric ', ' conv ');
Figure, imshow (blurred);
Title (' Blurred Image ')

% Add noise
V = 0.002;
Blurrednoisy = imnoise (blurred, ' Gaussian ', 0,v);
Figure, Imshow (Blurrednoisy)
Title (' Blurred and Noisy Image ')

P405

% recover images using Lucy-Richardson algorithm (5 iterations)
LUC1 = Deconvlucy (Blurrednoisy, PSF, 5);
Figure, Imshow (LUC1)

% recover images using Lucy-Richardson algorithm
Luc1_cell = Deconvlucy ({blurrednoisy}, PSF, 5);
Luc2_cell = Deconvlucy (Luc1_cell, PSF);
LUC2 = Im2uint8 (luc2_cell{2});
Figure, Imshow (LUC2);

Recovery effect of% control threshold
Dampar = Im2uint8 (3*sqrt (V));
LUC3 = Deconvlucy (Blurrednoisy, PSF, Dampar);
Figure, Imshow (LUC3);

P412

% to find a dark channel image of an image with a window size of 15*15
Imagergb = Imread (' picture.bmp ');
Imagergb = double (IMAGERGB);
Imagergb = imagergb./255;
Dark = Darkchannel (IMAGERGB);

% Save Intermediate Results
%imwrite (Dark, ' stadium_darkchannel.png ');

% Select the brightest 0.1% pixels in the dark channel to obtain atmospheric light
[M, n, ~] = size (IMAGERGB);
imsize = m * N;
NUMPX = Floor (imsize/1000);
Jdarkvec = reshape (dark,imsize,1);
Imvec = reshape (imagergb,imsize,3);

[Jdarkvec, indices] = sort (Jdarkvec);
Indices = indices (imsize-numpx+1:end);

Atmsum = zeros (1,3);
For ind = 1:NUMPX
Atmsum = atmsum + Imvec (Indices (IND),:);
End

atmospheric = atmsum/numpx;

% to solve the transmittance, and choose to retain a certain degree of haze through the Omega parameter to avoid damaging the realism

Omega = 0.95;

im = zeros (Size (IMAGERGB));

For ind = 1:3
Im (:,:, Ind) = Imagergb (:,:, Ind)./atmospheric (Ind);
End

Dark_2 = Darkchannel (IM);

t = 1-omega*dark_2;

% is used to save intermediate results
%imwrite (t, ' house_t.png ');

% more fine transmittance graphs through guided filtering
R = 60;
EPS = 10^-6;

refined_t = Guidedfilter_color (Imagergb, T, R, EPS);

% is used to save intermediate results
%imwrite (refined_t, ' house_r_t.png ');

Refinedradiance = getradiance (Atmospheric, Imagergb, refined_t);

% is used to save the result after the fog operation
Imwrite (refinedradiance, ' house_dehaze.png ');

P413

function Dark = Darkchannel (IMRGB)

R=imrgb (:,:, 1);
G=imrgb (:,:, 2);
B=imrgb (:,:, 3);

[m n] = size (r);
A = zeros (m,n);
For i = 1:m
for j = 1:n
A (i,j) = Min (R (i,j), G (i,j));
A (i,j) = min (A (i,j), B (i,j));
End
End

d = ones (15,15);
Fun = @ (block_struct) min (min (block_struct.data)) *d;
Dark = Blockproc (A, [all], fun);

Dark = Dark (1:m, 1:n);

Supplemental functions: The original book is limited to the length of the function is not fully listed (mainly the function that needs to be called when executing the above code), where the code of the guidance filter comes from the original author's personal homepage.

Supplemental function 1--boxfilter

function imdst = Boxfilter (IMSRC, R)

% Boxfilter O (1) Time box filtering using cumulative sum
%
%-Definition imdst (x, y) =sum (sum (IMSRC (x-r:x+r,y-r:y+r)));
%-Running time independent of R;
%-equivalent to the function:colfilt (IMSRC, [2*r+1, 2*r+1], ' sliding ', @sum);
%-but much faster.

[Hei, wid] = size (IMSRC);
IMDST = zeros (Size (IMSRC));

%cumulative sum over Y axis
Imcum = Cumsum (imsrc, 1);
%difference over Y axis
IMDST (1:r+1,:) = Imcum (1+r:2*r+1,:);
IMDST (r+2:hei-r,:) = Imcum (2*r+2:hei,:)-imcum (1:hei-2*r-1,:);
IMDST (Hei-r+1:hei,:) = Repmat (Imcum (Hei,:), [R, 1])-imcum (hei-2*r:hei-r-1,:);

%cumulative sum over X axis
Imcum = Cumsum (IMDST, 2);
%difference over Y axis
IMDST (:, 1:r+1) = Imcum (:, 1+r:2*r+1);
IMDST (:, r+2:wid-r) = Imcum (:, 2*r+2:wid)-Imcum (:, 1:wid-2*r-1);
IMDST (:, Wid-r+1:wid) = Repmat (Imcum (:, WID), [1, R])-Imcum (:, wid-2*r:wid-r-1);
End

Supplemental function 2--guidedfilter_color

function q = Guidedfilter_color (I, P, R, EPS)
% Guidedfilter_color O (1) Time implementation of guided filter using a COLOR image as the guidance.
%
%-Guidance image:i (should be a color (RGB) image)
%-filtering input image:p (should be a gray-scale/single channel image)
%-local window Radius:r
%-regularization parameter:eps

[Hei, wid] = size (p);
N = Boxfilter (Ones (Hei, wid), R); % the size of each local patch; N= (2r+1) ^2 except for boundary pixels.

Mean_i_r = Boxfilter (I (:,:, 1), R)./N;
Mean_i_g = Boxfilter (I (:,:, 2), R)./N;
Mean_i_b = Boxfilter (I (:,:, 3), R)./N;

Mean_p = Boxfilter (P, R)./N;

Mean_ip_r = Boxfilter (I (:,:, 1). *p, R)./N;
Mean_ip_g = Boxfilter (I (:,:, 2). *p, R)./N;
Mean_ip_b = Boxfilter (I (:,:, 3). *p, R)./N;

% covariance of (I, p) in each local patch.
Cov_ip_r = mean_ip_r-mean_i_r. * MEAN_P;
Cov_ip_g = mean_ip_g-mean_i_g. * MEAN_P;
Cov_ip_b = mean_ip_b-mean_i_b. * MEAN_P;

% variance of I in each of the local patch:the matrix Sigma in EQN (14).
% Note the variance in each local patch is a 3x3 symmetric matrix:
% RR, RG, RB
% Sigma = RG, GG, GB
% RB, GB, BB
VAR_I_RR = Boxfilter (I (:,:, 1). *i (:,:, 1), R)./n-mean_i_r. * MEAN_I_R;
Var_i_rg = Boxfilter (I (:,:, 1). *i (:,:, 2), R)./n-mean_i_r. * MEAN_I_G;
VAR_I_RB = Boxfilter (I (:,:, 1). *i (:,:, 3), R)./n-mean_i_r. * MEAN_I_B;
Var_i_gg = Boxfilter (I (:,:, 2). *i (:,:, 2), R)./n-mean_i_g. * MEAN_I_G;
VAR_I_GB = Boxfilter (I (:,:, 2). *i (:,:, 3), R)./n-mean_i_g. * MEAN_I_B;
VAR_I_BB = Boxfilter (I (:,:, 3). *i (:,:, 3), R)./n-mean_i_b. * MEAN_I_B;

A = zeros (Hei, WID, 3);
For Y=1:hei
For X=1:wid
Sigma = [Var_i_rr (y, x), Var_i_rg (y, x), Var_i_rb (y, x);
Var_i_rg (y, x), Var_i_gg (y, x), Var_i_gb (y, x);
Var_i_rb (y, x), Var_i_gb (y, x), Var_i_bb (y, x)];
%sigma = Sigma + eps * Eye (3);

COV_IP = [Cov_ip_r (y, x), Cov_ip_g (y, x), Cov_ip_b (y, x)];

A (y, x,:) = cov_ip * INV (Sigma + EPS * Eye (3)) and% Eqn. In the paper;
End
End

b = Mean_p-a (:,:, 1). * MEAN_I_R-A (:,:, 2). * MEAN_I_G-A (:,:, 3). * MEAN_I_B; % Eqn. (+) in the paper;

Q = (Boxfilter (A (:,:, 1), R). * I (:,:, 1) ...
+ Boxfilter (A (:,:, 2), R). * I (:,:, 2) ...
+ Boxfilter (A (:,:, 3), R). * I (:,:, 3) ...
+ Boxfilter (b, R))./N; % Eqn. (+) in the paper;
End

Supplemental function 3--getradiance

function J = getradiance (A,im,tmat)
T0 = 0.1;
J = zeros (Size (IM));
For ind = 1:3
J (:,:, Ind) = A (Ind) + (IM (:,:, Ind)-A (Ind))./max (TMAT,T0);
End

J = j./(max (max (J)));


(The code release is not finished, please wait for follow-up ...) )

"Digital image processing principle and practice (MATLAB version)" A book Code PART8

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.