Matlab 图像几何变换

MATLAB初学——Matlab实现图像的几何变换

1.图像比例缩放变换

主要函数:imresize()
实现代码

image = imread('test.bmp');
image_1 = imresize(image,0.5); %比例缩小0.5倍
image_2 = imresize(image,[320,410]); %非比例缩小(指定行数和列数)
image_3 = imresize(image,2); %比例放大1倍
figure(1)
imshow(image);
title('原图');
figure(2)
imshow(image_1);
title('比例缩小0.5倍');
figure(3)
imshow(image_2);
title('非比例缩小');
figure(4)
imshow(image_3);
title('比例放大');

2.图像平移变换

主要函数:translate(); imdilate(); imtranslate()
实现代码

image = imread('test.bmp');
se = translate(strel(1), [150 150]); %将一个平面结构化元素分别向下和向右移动150个位置
image_1 = imdilate(image,se); %利用膨胀函数平移图像
image_2 = imtranslate(image,[150,150],'OutputView','full'); %平移后保持图像完整
figure(1)
imshow(image);
title('原图');
figure(2)
imshow(image_1);
title('平移后的图像');
figure(3)
imshow(image_2);
title('平移后不被截去的图像');

3.图像镜像变换

主要函数:maketform(); imtransform();
实现代码

image = imread('test.bmp');
[height,width,dim] = size(image);
tform = maketform('affine',[-1 0 0;0 1 0;width 0 1]);
image_1 = imtransform(image,tform,'nearest');
tform2 = maketform('affine',[1 0 0;0 -1 0;0 height 1]);
image_2 = imtransform(image,tform2,'nearest');
subplot(1,3,1); imshow(image);  title('原图像');
subplot(1,3,2); imshow(image_1);  title('水平镜像');
subplot(1,3,3);  imshow(image_2);  title('垂直镜像');

4.图像旋转变换

主要函数:imrotate()
实现代码

image = imread('test.bmp');
image_1 = imrotate(image,40,'nearest','crop'); %最近邻插值
image_2 = imrotate(image,40,'nearest','loose'); 
figure; imshow(image); title('原图像');
figure; imshow(image_1); title('旋转后图像');
figure; imshow(image_2); title('旋转后不被截去图像');

5.图像绕某点(非中心点)旋转

自定义函数,构建变换矩阵,与原图像矩阵相乘,实现先平移后旋转功能
实现代码

function rotate(img)
[m,n] = size(img);
M = ones(m,n);
T1 = [1,0,0;0,1,0;-66,-66,1];  %平移至点P(66,66)
T2 = [cosd(60),-sind(60),0;sind(60),cosd(60),0;0,0,1]; %逆时针旋转60°
T =T2*T1;  %变换矩阵
for i=1:m
    for j=1:n
        p = floor([i,j,1]*T);
        if (p(1)<=m)&&(p(1)>0)&&(p(2)<=n)&&(p(2)>0) %限制范围 
         M(i,j) = img(p(1),p(2));   
        else 
        M(i,j) = 0;  
        end
    end
end
figure; imshow(M,[]); title('绕点P逆时针旋转60°');

image = imread('test.bmp');
image_1 = rgb2gray(image);
figure
subimage(image_1); 
title('原图');
rotate(image_1)

你可能感兴趣的:(matlab,图像处理)