
《数字图像处理》实验指导书201403.doc
8页11.直方图均衡化clear all; close all % Clear the MATLAB workspace of any variables % and close open figure windows.I = imread('pout.tif'); % Reads the sample images ‘ pout.tif’, and stores it in imshow(I) % an array named I.display the imagefigure, imhist(I) % Create a histogram of the image and display it in % a new figure window.[I2,T] = histeq(I); % Histogram equalization.figure, imshow(I2) % Display the new equalized image, I2, in a new figure window.figure, imhist(I2) % Create a histogram of the equalized image I2.figure,plot((0:255)/255,T); % plot the transformation curve.imwrite (I2, 'pout2.png'); % Write the newly adjusted image I2 to a disk file named% ‘pout2.png’.imfinfo('pout2.png') % Check the contents of the newly written file2.直接灰度变换clear all; close allI = imread('cameraman.tif');J = imadjust(I,[0 0.2],[0.5 1]);imshow(I)figure, imshow(J)[X,map] = imread('forest.tif');figure,imshow(X,map)I2 = ind2gray(X,map);J2 = imadjust(I2,[],[],0.5); figure,imshow(I2)figure, imshow(J2)J3 = imadjust(I2,[],[],1.5); figure, imshow(J3)help imadjust % Display the imadjust() function information.3.空域平滑滤波(模糊、去噪)clear all; close allI = imread('eight.tif');h1 = ones(3,3) / 9;h2 = ones(5,5) / 25;2I1 = imfilter(I,h1);I2 = imfilter(I,h2);figure(1), imshow(I), title('Original Image');figure(2), imshow(I1), title('Filtered Image With 3*3 ')figure(3), imshow(I2), title('Filtered Image With 5*5 ')% 加入 Gaussian 噪声J1 = imnoise(I,'gaussian',0,0.005);% 加入椒盐噪声J2 = imnoise(I,'salt % 对J1、J2进行平均值平滑滤波K1 = imfilter(J1,fspecial('average',3));K2 = imfilter(J2,fspecial('average',3));figure(4);subplot(2,2,1), imshow(J1) , title('gaussian');subplot(2,2,2), imshow(J2), title('salt subplot(2,2,3), imshow(K1), title('average ');subplot(2,2,4), imshow(K2);% 对J1、J2进行中值滤波K3 = medfilt2(J1,[3 3]);K4 = medfilt2(J2,[3 3]);figure(5);subplot(2,2,1), imshow(J1) , title('gaussian');subplot(2,2,2), imshow(J2), title('salt subplot(2,2,3), imshow(K3), title(' Median filtering ');subplot(2,2,4), imshow(K4)4.空域锐化滤波clear all; close allI = imread('moon.tif');w=fspecial('laplacian',0)w8=[1,1,1;1,-8,1;1,1,1]I1= imfilter(I,w, 'replicate');figure(1); imshow(I), title('Original Image');figure(2), imshow(I1), title('Laplacian Image');f = im2double(I);f1= imfilter(f,w, 'replicate');3figure(3), imshow(f1,[]), title('Laplacian Image');f2= imfilter(f,w8, 'replicate');f4 = f-f1;f8 = f-f2;figure(4), imshow(f4);figure(5), imshow(f8);。
1.傅立叶变换(1) 简单人工二值图像clear all;close all;clc;f = zeros(50,50);f(15:35,23:28) = 1;figure(1), imshow(f,'notruesize')F = fft2(f,128,128);F1 = fftshift(F);figure(2), imshow(log(abs(F1)), [-1 5]); colormap(gray); colorbarfigure(3), mesh(1:128,1:128, abs(F1)); colormap(gray); colorbarF2 = fft2(imrotate(f,90),128,128);F3 = fftshift(F2);figure(4), imshow(imrotate(f,90),'notruesize')figure(5), imshow(log(abs(F3)), [-1 5]); colormap(gray); colorbarfigure(6), mesh(1:128,1:128, abs(F3)); colormap(gray); colorbar(2)Laplacian 图像锐化增强的频域实现clear all;close all;clc;I = imread('moon.tif');I = im2double(I); %标定到[0-1]imshow(I);MN=size(I);PQ=2*MN;F=fft2(I,PQ(1),PQ(2));F1 =abs(F);figure,imshow(F1,[]);F2=log(1+abs(F));figure,imshow(F2,[]);F3=log(1+abs(fftshift(F)));4figure,imshow(F3,[]);h = fspecial('laplacian');H = fft2 (h,PQ(1),PQ(2));H = fftshift(H);H1=log(1+abs(H));figure,imshow(H1,[]);% calculate the laplacian imageG = fftshift(F).*H;g = real(ifft2(ifftshift(G)));g1 = imcrop(g,[1,1,MN(2)-1,MN(1)-1]); figure, imshow(mat2gray(g1),[]);% calculate the enhanced image using laplacian operatorg2 = g1/max(max(abs(g1))); %标定到[0-1]G = I-g2;figure, imshow(G);(3) 频率域平滑滤波close all;clear all;clc;I = imread('text.png');imshow(I);MN=size(I);PQ=2*MN;F = fft2(I,PQ(1),PQ(2));h = fspecial('average');H = fft2 (h,PQ(1),PQ(2));I1 = real(ifft2(F .* H));G = imcrop(I1, [1,1,MN(2)-1, MN(1)-1]);figure, imshow(G,[]); %Display, scaling data to appropriate range.2.离散余弦变换(DCT)% 对cameraman.tif图像的离散余弦变换及逆变换重建clear all, close allf=imread('cameraman.tif'); %读一幅 cameraman.tifimshow(f);F=dct2(f); %做余弦变换AbsFT=abs(F);figure, imshow(log(AbsFT));FinvT=idct2(F); %做余弦反变换figure, imshow(mat2gray(FinvT));5%仅保留余弦变换频谱的左上角50*50个数据,然后做余弦反变换,观察输出图像F1=F;[m,n]=size(F1);F1(50:m,50:n)=0;AbsFT=abs(F1);figure, imshow(log(AbsFT));FinvT=idct2(F1); %做余弦反变换figure, imshow(mat2gray(FinvT));1.膨胀与腐蚀(Dilation and Erosion)(1)对简单二值图像进行膨胀与腐蚀clear all, close allBW = zeros(9,10); BW(4:6,4:7) = 1;BWSE = strel('square',3) BW1 = imdilate(BW,SE)BW2 = imerode (BW,SE)figure(1),subplot(1,2,1), imshow(BW,'notruesize'), title(' Original Image ');subplot(1,2,2), imshow(BW1,'notruesize'), title(' Dilated Image ');figure(2),subplot(1,2,1), imshow(BW,'notruesize'), title(' Original Image ');subplot(1,2,2), imshow(BW2,'notruesize'), title(' Eroded Image ');(2)对文本图像进行膨胀与腐蚀clear all, close allI = imread('Fig0907(a)(text_gaps_1_and_2_pixels).tif'); % 说明:上述图片可从Gonzalez网站下载SE = [0,1,0;1,1,1;0,1,0]BW1 = imdilate(I, SE);BW2 = ime。
