Matlab学习3-图像处理之镜像、错切、透视

Matlab学习3-图像处理之翻转、镜像、错切、透视
使用flipdim、maketform、imtransform、interp2
这里用的环境是matlab2020
1、图像水平翻转、镜像翻转
效果如下:
在这里插入图片描述
test.m代码如下:

img1=imread("img.jpg");
%参数2是行做逆序
img2=flipdim(img1,2);
%1是列做逆序
img3=flipdim(img1,1);
subplot(1,3,1),imshow(img1),xlabel("原图");
subplot(1,3,2),imshow(img2),xlabel("水平反转");
subplot(1,3,3),imshow(img3),xlabel("镜像反转");

2、图像错切,水平错切、垂直错切、水平垂直错切
效果如下:
在这里插入图片描述
test.m代码如下:

img1=imread("img.jpg");
%变换矩阵
Ju=[1 0 0
    -0.5 1 0
    0 0 1]
%空间结构类型变换
tform=maketform("affine",Ju);
img2=imtransform(img1,tform);

Ju=[1 0.5 0
    0 1 0
    0 0 1]
tform=maketform("affine",Ju);
img3=imtransform(img1,tform);

Ju=[1 0.5 0
    0.5 1 0
    0 0 1]
tform=maketform("affine",Ju);
img4=imtransform(img1,tform);

subplot(2,2,1),imshow(img1),xlabel("原图");
subplot(2,2,2),imshow(img2),xlabel("水平错切");
subplot(2,2,3),imshow(img3),xlabel("垂直错切");
subplot(2,2,4),imshow(img4),xlabel("水平垂直错切");

3、图像透视
效果如下:
在这里插入图片描述

test.m代码如下:

img1=imread("img.jpg");
udata=[0 1];
vdata=[0 1];
%变换矩阵
T=[0 0 
   1 0
   1 1
   0 1]
T1=[-4 2 
   -8 -3
   1 -8
   6 6]
% 创建变换结构,透视类型
tform=maketform('projective',T,T1);
%xdata与ydata均是二元向量中,分别对应x轴与y轴 bicubic双立方插值
[img2,xdata,ydata]=imtransform(img1,tform,'bicubic','udata',udata,'vdata',vdata,'size',size(img1),'fill',0);
%255是白色
[img3,xdata,ydata]=imtransform(img1,tform,'bicubic','udata',udata,'vdata',vdata,'size',size(img1),'fill',255);
subplot(1,3,1),imshow(img1),axis on,xlabel("原图");
subplot(1,3,2),imshow(img2),axis on,xlabel("透视图1");
subplot(1,3,3),imshow(img3),axis on,xlabel("透视图2");

4、图像失真
效果如下:

test.m代码如下:

img1=imread("img.jpg");

subplot(2,2,1),imshow(img1),xlabel("原图");
times=8;
%缩小,分辨率不变,近邻插值算法,近邻灰度赋值
img2=imresize(img1,1/times);
subplot(2,2,2),imshow(img2),xlabel("失真图像");
%interp空间变换插值方法,二维插值函数,灰度插值运算
% img2=reshape();
img3=interp2(double(img2),'nearest');
subplot(2,2,3),imshow(img3),xlabel("最近邻插值方法填充的效果");

素材如下:
gitee:https://gitee.com/CYFreud/matlab/tree/master/image_processing/demo3

猜你喜欢

转载自blog.csdn.net/CHengYuP/article/details/123785349