Enhancement_set2

  • 格式:pdf
  • 大小:1.20 MB
  • 文档页数:20

Text figure (4.2) 1D Discrete Fourier Transform%Matlab CodeA = 1; K = 10; M = 100;f1 = zeros(1,M); f2 = zeros(1,M);f1(1:K) = A; f2(1:2*K) = A;fft1 = abs(fftshift(fft(f1)))/M;fft2 = abs(fftshift(fft(f2)))/M;figure;subplot(2,2,1);plot(f1); title('Filter for M=100, K=10, A=1'); xlabel('x');ylabel('f(x)'); axis([0 M 0 1.1*A]);subplot(2,2,2);plot(fft1); title('Fourier Transform for Filter in (a)'); xlabel('u'); ylabel('|F(u)|'); axis([0 M 0 1.1*[A*K]/M]);subplot(2,2,3);plot(f2); title('Filter for M=100, K=10, A=1'); xlabel('x');ylabel('f(x)'); axis([0 M 0 1.1*A]);subplot(2,2,4);plot(fft2); title('Fourier Transform for Filter in (c)'); xlabel('u'); ylabel('|F(u)|'); axis([0 M 0 1.1*[2*A*K]/M]);Text figure (4.3) 2D Discrete Fourier Transform4.4)%Matlab Codeclose all; clear all;img = imread('Fig4.03(a).jpg');imgfft = abs(fftshift(fft2(img)));figure;subplot(1,2,1);imshow(img);subplot(1,2,2);imshow(imscale01(log10(imgfft+1)));Text figure (4.6) 2D Fourier Notch Filteringclose all; clear all;img = imread('Fig4.04(a).jpg');imgfft = fftshift(fft2(img));imgfft(size(img,1)/2+1, size(img,2)/2+1) = 0;imgfilt = abs((ifft2(imgfft)));figure;imshow(imscale01(imgfilt));Text figure (4.7) High Pass / Low Pass 2D Filteringclose all; clear all;img = imread('Fig4.04(a).jpg');[x,y] = size(img);lp = zeros(size(img));cx = x/2; cy = y/2; std = 30;for i=1:x; for j=1:y;lp(i,j) = exp(-[(i-cx)^2 + (j-cy)^2]/std^2);end endhp = 1-lp;lpplot = lp(1:8:x, 1:8:y); hpplot = hp(1:8:x, 1:8:y);imglp = fftshift(fft2(img)); imglp = imglp .* lp;imghp = imglp; imghp = imghp .* hp;imglp = abs((ifft2(imglp))); imghp = abs((ifft2(imghp))); figure;subplot(2,2,1); mesh(lpplot); axis tight; colormap(gray(1)); subplot(2,2,2); imshow(imscale01(imglp));subplot(2,2,3); mesh(hpplot); axis tight; colormap(gray(1)); subplot(2,2,4); imshow(imscale01(imghp));Text figure (4.8) Modified 2D High Pass Filter%Matlab Codeclose all; clear all;img = imread('Fig4.04(a).jpg');[x,y] = size(img);lp = zeros(size(img));cx = x/2; cy = y/2; std = 30;for i=1:x;for j=1:y;lp(i,j) = exp(-[(i-cx)^2 + (j-cy)^2]/std^2); endendhp = 1-lp; hp = hp + 0.5*max(hp(:));imghp = fftshift(fft2(img)); imghp = imghp .* hp; imghp = abs((ifft2(imghp)));figure; imshow(imscale01(imghp));Text figure (4.12) 2D Ideal Low-pass Filtering %Matlab Codeclose all; clear all;img = double(imread('Fig4.11(a).jpg')); [x,y] = size(img);lp5 = zeros(size(img)); lp15 = zeros(size(img));lp30 = zeros(size(img)); lp80 = zeros(size(img));lp230 = zeros(size(img));cx = x/2; cy = y/2; std = 30;for i=1:x;for j=1:y;if(sqrt([i-cx]^2 + [j-cy]^2) < 5)lp5(i,j) = 1;endif(sqrt([i-cx]^2 + [j-cy]^2) < 15)lp15(i,j) = 1;endif(sqrt([i-cx]^2 + [j-cy]^2) < 30)lp30(i,j) = 1;endif(sqrt([i-cx]^2 + [j-cy]^2) < 80)lp80(i,j) = 1;endif(sqrt([i-cx]^2 + [j-cy]^2) < 230)lp230(i,j) = 1;endendendimgfft = fftshift(fft2(img));lp5 = abs(ifft2(imgfft .* lp5)); lp15 = abs(ifft2(imgfft .* lp15));lp30 = abs(ifft2(imgfft .* lp30)); lp80 = abs(ifft2(imgfft .* lp80));lp230 = abs(ifft2(imgfft .* lp230));figure;subplot(3,2,1); imshow(imscale01(img)); subplot(3,2,2); imshow(imscale01(lp5)); subplot(3,2,3); imshow(imscale01(lp15)); subplot(3,2,4); imshow(imscale01(lp30)); subplot(3,2,5); imshow(imscale01(lp80)); subplot(3,2,6); imshow(imscale01(lp230));Text figure (4.13) Ideal Low-Pass Filter Response%Matlab Codeclose all; clear all;img = zeros(500,500);x = 500; y = 500;cx = 250; cy = 250;for i=1:x;for j=1:y;if(sqrt([i-cx]^2 + [j-cy]^2) < 5)img(i,j) = 1;endendendimgfft = (fftshift(ifft2(img)));a = ones(1,500);a(1:2:500) = -1;plota = real(imgfft(250,:)).*a;img2 = zeros(size(img));img2(128,128) = 1; img2(128, 500-128) = 1; img2(250, 250) = 1; img2(500-128, 128) = 1; img2(500-128, 500-128) = 1; img3 = conv2(img2, imgfft,'same');idx = 1:size(img,2)+1:length(img(:));plotb = real(img3(idx));figure;subplot(3,2,1); imshow(imscale01(img));subplot(3,2,2); plot((plota)); axis tight;subplot(3,2,3); imshow(imscale01(0.5*log10(abs(imgfft)+1)));subplot(3,2,4); imshow(imscale01(img2));subplot(3,2,5); plot((plotb)); axis tight;subplot(3,2,6); imshow(imscale01(0.5*log10(abs(img3)+1)));%Matlab Codeimg = im2double(imread('Fig4.11(a).jpg'));imgfft = fftshift(fft2(img));H5 = zeros(500,500); H15 = H5; H30 = H5; H80 = H5;H230 = H5; x = 500; y = 500; cx = x/2; cy = y/2;for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);H5(i,j) = 1 / (1 + [D(i,j)/5]^(4));H15(i,j) = 1 / (1 + [D(i,j)/15]^(4));H30(i,j) = 1 / (1 + [D(i,j)/30]^(4));H80(i,j) = 1 / (1 + [D(i,j)/80]^(4));H230(i,j) = 1 / (1 + [D(i,j)/230]^(4));endendimg5 = abs(ifft2(imgfft .* H5)); img15 = abs(ifft2(imgfft .* H15));img30 = abs(ifft2(imgfft .* H30)); img80 = abs(ifft2(imgfft .* H80));img230 = abs(ifft2(imgfft .* H230));figure;subplot(3,2,1); imshow(imscale01(img)); subplot(3,2,2); imshow(imscale01(img5)); subplot(3,2,3); imshow(imscale01(img15)); subplot(3,2,4); imshow(imscale01(img30)); subplot(3,2,5); imshow(imscale01(img80)); subplot(3,2,6); imshow(imscale01(img230));Text figure (4.16) Butterworth Low-Pass Filter Response%Matlab Codeimg = im2double(imread('Fig4.11(a).jpg'));imgfft = fftshift(fft2(img));H1 = zeros(500,500); H2 = H1; H5 = H1; H20 = H1;H230 = H5; x = 500; y = 500; cx = x/2; cy = y/2;for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);H1(i,j) = 1 / (1 + [D(i,j)/5]^(2));H2(i,j) = 1 / (1 + [D(i,j)/5]^(4));H5(i,j) = 1 / (1 + [D(i,j)/5]^(10));H20(i,j) = 1 / (1 + [D(i,j)/5]^(40));endendh1 = abs(fftshift(ifft2(H1))); h2 = abs(fftshift(ifft2(H2)));h5 = abs(fftshift(ifft2(H5))); h20 = abs(fftshift(ifft2(H20))); figure;subplot(2,4,1); imshow(imscale01(20*log10(1+h1)));subplot(2,4,2); imshow(imscale01(20*log10(1+h2)));subplot(2,4,3); imshow(imscale01(20*log10(1+h5)));subplot(2,4,4); imshow(imscale01(20*log10(1+h20)));subplot(2,4,5); plot(h1(250,:)); axis tight;subplot(2,4,6); plot(h2(250,:)); axis tight;subplot(2,4,7); plot(h5(250,:)); axis tight;Text figure (4.18) Gaussian Low-Pass Filtering %Matlab Codeimg = im2double(imread('Fig4.11(a).jpg'));imgfft = fftshift(fft2(img));H5 = zeros(500,500); H15 = H5; H30 = H5; H80 = H5; H230 = H5;x = 500; y = 500; cx = x/2; cy = y/2;for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);endendH5 = exp(-.5*[D/5].^2); H15 = exp(-.5*[D/15].^2);H30 = exp(-.5*[D/30].^2); H80 = exp(-.5*[D/80].^2);H230 = exp(-.5*[D/230].^2);h5 = abs(ifft2(H5.*imgfft)); h15 = abs(ifft2(H15.*imgfft));h30 = abs(ifft2(H30.*imgfft)); h80 = abs(ifft2(H80.*imgfft));h230 = abs(ifft2(H230.*imgfft));figure;subplot(3,2,1); imshow(imscale01(img)); subplot(3,2,2); imshow(imscale01(h5)); subplot(3,2,3); imshow(imscale01(h15)); subplot(3,2,4); imshow(imscale01(h30)); subplot(3,2,5); imshow(imscale01(h80)); subplot(3,2,6); imshow(imscale01(h230));Text figure (4.20) Gaussian Low-Pass Filtering%Matlab Codeimg = im2double(imread('Fig4.20(a).jpg'));imgfft = fftshift(fft2(img));[x,y] = size(img); cx = x/2; cy = y/2;for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);endendH100 = exp(-.5*[D/100].^2); H80 = exp(-.5*[D/80].^2);h100 = abs(ifft2(H100.*imgfft)); h80 = abs(ifft2(H80.*imgfft)); figure;subplot(2,3,1); imshow(imscale01(img(380:460,380:460)));subplot(2,3,2); imshow(imscale01(h100(380:460,380:460)));subplot(2,3,3); imshow(imscale01(h80(380:460,380:460)));subplot(2,3,4); imshow(imscale01(img));subplot(2,3,5); imshow(imscale01(h100));subplot(2,3,6); imshow(imscale01(h80));Text figure (4.21) Gaussian Low-Pass Filtering %Matlab Codeimg = im2double(imread('Fig4.21(a).jpg'));imgfft = fftshift(fft2(img));[x,y] = size(img); cx = x/2; cy = y/2;for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);endendH30 = exp(-.5*[D/30].^2); H10 = exp(-.5*[D/10].^2);h30 = abs(ifft2(H30.*imgfft)); h10 = abs(ifft2(H10.*imgfft)); figure;subplot(1,3,1); imshow(imscale01(img));subplot(1,3,2); imshow(imscale01(h30));subplot(1,3,3); imshow(imscale01(h10));Text figure (4.24) Ideal High-pass Filtering %Matlab Codeimg = double(imread('Fig4.11(a).jpg')); [x,y] = size(img);hp15 = zeros(size(img)); hp30 = zeros(size(img));hp80 = zeros(size(img));cx = x/2; cy = y/2; std = 30;for i=1:x;for j=1:y;if(sqrt([i-cx]^2 + [j-cy]^2) > 15)hp15(i,j) = 1;endif(sqrt([i-cx]^2 + [j-cy]^2) > 30)hp30(i,j) = 1;endif(sqrt([i-cx]^2 + [j-cy]^2) > 80)hp80(i,j) = 1;endendendimgfft = fftshift(fft2(img));hp15 = abs(ifft2(imgfft .* hp15));hp30 = abs(ifft2(imgfft .* hp30));hp80 = abs(ifft2(imgfft .* hp80));figure;subplot(1,3,1); imshow(imscale01(hp15));subplot(1,3,2); imshow(imscale01(hp30));subplot(1,3,3); imshow(imscale01(hp80));Text figure (4.25) Butterworth High-Pass Filtering %Matlab Codeimg = im2double(imread('Fig4.11(a).jpg'));imgfft = fftshift(fft2(img));H15 = zeros(500,500); H30 = H15; H80 = H15;x = 500; y = 500; cx = x/2; cy = y/2;for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);H15(i,j) = 1 / (1 + [15/D(i,j)]^(4));H30(i,j) = 1 / (1 + [30/D(i,j)]^(4));H80(i,j) = 1 / (1 + [80/D(i,j)]^(4));endendimg15 = abs(ifft2(imgfft .* H15));img30 = abs(ifft2(imgfft .* H30));img80 = abs(ifft2(imgfft .* H80));figure;subplot(1,3,1); imshow(imscale01(img15));subplot(1,3,2); imshow(imscale01(img30));subplot(1,3,3); imshow(imscale01(img80));Text figure (4.26) Gaussian High-Pass Filtering %Matlab Codeimg = im2double(imread('Fig4.11(a).jpg'));imgfft = fftshift(fft2(img));H15 = zeros(500,500); H30 = H15; H80 = H15;x = 500; y = 500; cx = x/2; cy = y/2;for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);endendH15 = 1-exp(-.5*[D/15].^2);H30 = 1-exp(-.5*[D/30].^2);H80 = 1-exp(-.5*[D/80].^2);h15 = abs(ifft2(H15.*imgfft));h30 = abs(ifft2(H30.*imgfft));h80 = abs(ifft2(H80.*imgfft));figure;subplot(1,3,1); imshow(imscale01(h15));subplot(1,3,2); imshow(imscale01(h30)); subplot(1,3,3); imshow(imscale01(h80));Text figure (4.28) Laplacian Filtering%Matlab Codeimg = im2double(imread('Fig4.28(a).jpg'));imgfft = fftshift(fft2(img));[x,y] = size(img); cx = x/2; cy = y/2;for i=1:x;for j=1:y;D(i,j) = -((i-cx)^2 + (j-cy)^2);endendlapimg = ifft2(D.*imgfft);figure;subplot(2,2,1); imshow(imscale01(img));subplot(2,2,2); imshow(imscale01(abs(lapimg)));subplot(2,2,3); imshow(imscale01((real(lapimg))));subplot(2,2,4); imshow(imscale01(real(img - abs(lapimg))));Text figure (4.29) High-boost Filtering%Matlab Code – Note: I used a Gaussian HP Filter since I don’t trust my Laplacian close all; clear all;img = im2double(imread('Fig4.29(a).jpg'));imgfft = fftshift(fft2(img));[x,y] = size(img); cx = x/2; cy = y/2;k = zeros(x,y);for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);k(i,j) = (-1)^(i+j);endendH = 1-exp(-.5*[D/50].^2);himg = real(ifft2(H.*imgfft)).*k;img1 = img + himg;img17 = 1.7*img + himg;figure;subplot(2,2,1); imshow(imscale01(img));subplot(2,2,2); imshow(imscale01(himg));subplot(2,2,3); imshow(imscale01(img1));subplot(2,2,4); imshow(imscale01(img17));Text figure (4.30) Multiple-operation Filtering%Matlab Codeclose all; clear all;img = im2double(imread('Fig4.30(a).jpg'));imgfft = fftshift(fft2(img));BHPF = zeros(size(imgfft));x = size(BHPF,1); y = size(BHPF,2); cx = x/2; cy = y/2;Do = floor(0.05*x);for i=1:x;for j=1:y;D(i,j) = sqrt([i-cx]^2 + [j-cy]^2);if(D(i,j) == 0) D(i,j)=0.01; endBHPF(i,j) = 1 / (1 + [Do/D(i,j)]^(4));endendimgBHPF = abs(ifft2(imgfft .* (BHPF)));imgHFE = abs(ifft2(imgfft .* (0.5 + 2*BHPF)));imgHFEeq = floor(255*imscale01(imgHFE));[hist1, bins1] = hist(double(imgHFEeq(:)),0:255);hist1 = hist1./length(imgHFEeq(:));CDF1 = cumsum(hist1);img1eq = zeros(size(imgHFEeq));for i=0:255img1eq(find(imgHFEeq==i)) = CDF1(i+1);endfigure;subplot(2,2,1); imshow(imscale01(img));subplot(2,2,2); imshow(imgBHPF+.4);subplot(2,2,3); imshow(imscale01(imgHFE));subplot(2,2,4); imshow(imscale01(img1eq));Text figure (4.33) Homomorphic Filtering %Matlab Codeclose all; clear all;img = im2double(imread('Fig4.29(a).jpg'));img2 = log(img+1);imgfft = fftshift(fft2(img2));H = zeros(size(img));[x,y]=size(H); cx = x/2; cy = y/2;Gl = .5; Gh = 2; Gdiff = Gh - Gl; c=1; Do = 10;k = zeros(x,y);for i=1:xfor j=1:yD(i,j) = sqrt([i-cx]^2 + [j-cy]^2);H(i,j) = Gdiff*[1 - exp(-c*(D(i,j)/Do)^2)] + Gl;k(i,j) = (-1)^(i+j);endendimgen = real(exp(ifft2(H.*imgfft))-1).*k;figure;subplot(1,2,1); imshow(imscale01(img));title('Original Image');subplot(1,2,2); imshow(imscale01(imgen));title('Homomorphic Image: c=1, D_o=10, G_l=.5, G_h=2');Original Image Homomorphic Image: c=1, D o=10, G l=.5, G h=2Acknowledge: I would like to thank my former student Brad Ratliff for providing these images.。