天天看點

數字圖像處理 第二章

旋轉

i=imread('cs.jpg');
i=rgb2gray(i);
b=imrotate(i,30,'nearest');
c=imrotate(i,50,'bilinear');
d=imrotate(i,80,'bicubic');
subplot(2,2,1),imshow(i);
title('原圖像');
subplot(1,3,1),imshow(b);
title('最鄰近內插補點');
subplot(1,3,2),imshow(c);
title('雙線性內插補點');
subplot(1,3,3);imshow(d);
title('雙三次內插補點');

           

雙線性插值

%%雙線性插值實作
original=imread('cs.jpg');
[height,width,channel] = size(original); 
zmf=0.5;
new_height = round(height*zmf); % 計算縮放後的圖像高度,最近取整 
new_width = round(width*zmf); % 計算縮放後的圖像寬度,最近取整
new_img = zeros(new_height,new_width,channel); % 建立新圖像 
img_scale = zeros(height+2,width+2,channel); % 為了邊界點考慮的
img_scale(2:height+1,2:width+1,:) = original;
img_scale(1,2:width+1,:) = original(1,:,:); 
img_scale(height+2,2:width+1,:) = original(height,:,:); 
img_scale(2:height+1,1,:) = original(:,1,:);
img_scale(2:height+1,width+2,:) = original(:,width,:);
img_scale(1,1,:) = original(1,1,:); 
img_scale(1,width+2,:) = original(1,width,:);
img_scale(height+2,1,:) = original(height,1,:); 
img_scale(height+2,width+2,:) = original(height,width,:);
for zj = 1:new_width % 對圖像進行按列逐元素掃描 
    for zi = 1:new_height % (zi,zj)表示在新圖中的坐标,(ii,jj)表示在原圖中的坐标 % 注意:(ii,jj)不一定是整數 
        ii = (zi-1)/zmf; jj = (zj-1)/zmf; 
        i = floor(ii); j = floor(jj); % 向下取整得到在原圖中坐标的整數部分
        u = ii - i; v = jj - j; % 得到在原圖中坐标的小數部分 
        i = i + 1; 
        j = j + 1; 
        new_img(zi,zj,:) = (1-u)*(1-v)*img_scale(i,j,:) + u*(1-v)*img_scale(i,j+1,:)... 
            + (1-u)*v*img_scale(i+1,j,:) + u*v*img_scale(i+1,j+1,:); 
    end 
end
new_img = uint8(new_img);
subplot 121 ,imshow(original);
subplot 122,imshow(new_img);

 


           
%雙線性插值
function scaler_bilinear_matlab()
%配置輸入輸出
I=imread('E:\哈利.jpg');
[width_sr,height_sr,L]=size(I);
K=str2double(inputdlg('please input scale factor (must between 0.2 - 5.0)','INPUT scale factor',1,{'0.5'}));
width =K*width_sr;
height=K*height_sr;
O = uint8(zeros(floor(width),floor(height),L));%size 必須為整數
widthscale=width_sr/width;
heightscale=height_sr/height;

for x=5:width-5
    for y=5:height-5
        xx=x*widthscale;
        yy=y*heightscale;
        
        if(xx/double(uint16(xx)==1.0))&(yy/double(uint16(yy)==1.0))
            O(x,y,:)=I(int16(xx),int16(yy));
        else
            a=double(uint16(xx));
            b=double(uint16(yy));
            x11=double(I(a,b,:));
            x12=double(I(a,b+1,:));
            x21=double(I(a+1,b,:));
            x22=double(I(a+1,b+1,:));
            
            O(x,y,:)=uint8((b+1-yy)*(xx-a)*x21+(a+1-xx)*x11+(yy-b)*((xx-a)*x22+(a+1-xx)*x12));%雙線性插值計算公式
        end
    end
end
figure,imshow(I);title('輸入圖像')
figure,imshow(uint8(O));title('輸出圖像')
            

           

仿射變換

%%仿射變換
A = imread('lena.jpg');
[height,width,dim] = size(A);
tform = maketform('affine',[-1 0 0;0 1 0;width 0 1]);
B = imtransform(A,tform,'nearest');
tform2 = maketform('affine',[1 0 0;0 -1 0;0 height 1]);
C = imtransform(A,tform2,'nearest');
figure;
subplot(1,3,1),imshow(A);title('原圖像');
subplot(1,3,2),imshow(B);title('lena水準鏡像');
subplot(1,3,3),imshow(C);title('lena垂直鏡像');

tform = maketform('affine',[0 1 0;1 0 0;0 0 1]);
B = imtransform(A,tform,'nearest');
figure;
subplot(1,2,1),imshow(A);title('原圖像');
subplot(1,2,2),imshow(B);title('轉置後圖像');

B = imrotate(A,30,'nearest','crop');
figure;
subplot(1,2,1),imshow(A);title('原圖像');
subplot(1,2,2),imshow(B);title('逆時針中心旋轉30度');



           
%%第二章亮度變換與空間濾波

%%gamma變換

f = imread('lena.jpg');

figure;          %打開新視窗
subplot 221;imshow(f);title('lena');
subplot 222;imshow(imadjust(f,[],[],0.5));title('Gamma 0.5');
subplot 223;imshow(imadjust(f,[],[],1.5));title('Gamma 1.5');
subplot 224;imshow(imadjust(f,[],[],3));title('Gamma 3');

%%對數變換,壓縮亮,增強暗部

f = imread('直方圖.gif');
F = fft2(im2double(f));
F = fftshift(F);   %FFT頻譜平移
F = abs(F);
T = 2*log(F+1);      %頻譜對數變換
figure,subplot 121 ;imshow(F,[]);title('未經變換的頻譜');
subplot 122;imshow(T,[]);title('對數變換後');

%%直方圖均衡化
g = histeq(f,255);  %f為輸入圖像 nlev是為輸入圖像指定的灰階級數
[M,N] = size(f);           
[counts1,x1] = imhist(f,32); %計算有32個小區間的灰階直方圖
counts1 = counts1/M/N;       %計算歸一化灰階直方圖各區間的值
[M,N] = size(g);           
[counts2,x2] = imhist(g,32); %計算有32個小區間的灰階直方圖
counts2 = counts2/M/N;       %計算歸一化灰階直方圖各區間的值
figure;subplot 221,imshow(f);title('原圖');
subplot 222 ;stem(x1,counts1); title('原圖直方圖')           %繪制歸一化直方圖
subplot 223,imshow(g);title('均衡化圖');
subplot 224 ;stem(x2,counts2); title('均衡化直方圖') ;

%%圖像平移

se = translate(strel(f),[180 190]);%strel用來建立形态學結構元素 ,【180 190】分别表示結構元素y,x方向平移
B = imdilate(f,se);
figure;subplot(1,2,1),subimage(f);title('原圖像');
subplot(1,2,2),subimage(B);title('平移後圖像');






           
%%噪聲濾波

A = imread('lena.jpg');
B = imnoise(A,'gaussian',0,0.02);%imnoise(f,'gaussian',m,var)中的方差是實際方差除以灰階級的平方,是以如果标準差25,var=25^2/255^2=0.0096
C = imnoise(A,'salt & pepper',0.02);
figure;
subplot(1,3,1),imshow(A);title('原圖像');
subplot(1,3,2),imshow(B);title('添加高斯白噪聲');
subplot(1,3,3),imshow(C);title('添加椒鹽白噪聲');

%%圖像銳化

I=double(A);%雙精度化
w1=[-1 0;0 1];
w2=[0 -1;1 0];
G1=imfilter(I,w1,'corr','replicate');%正45°梯度
G2=imfilter(I,w2,'corr','replicate');%負45°梯度
G=abs(G1)+abs(G2);%計算Robert梯度
figure;
subplot(1,3,1),imshow(G,[]);title('Robert梯度');
subplot(1,3,2),imshow(abs(G1),[]);title('正45°梯度');
subplot(1,3,3),imshow(abs(G2),[]);title('負45°梯度');


Id=double(I);%雙精度化
h_1=fspecial('log',5,0.5);%大小為5,sigma=0.5的LOG算子
I_1=imfilter(Id,h_1,'corr','replicate');
h_2=fspecial('log',5,2);%大小為5,sigma=2的LOG算子
I_2=imfilter(Id,h_2,'corr','replicate');
figure;
subplot(1,2,1),imshow(uint8(abs(I_1)),[]);title('大小為5,sigma=0.5的LOG算子');
subplot(1,2,2),imshow(uint8(abs(I_2)),[]);title('大小為5,sigma=2的LOG算子');