数字图像处理 第二章

旋转

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算子');


猜你喜欢

转载自blog.csdn.net/qq_41244435/article/details/83303809