图像的几何变换(平移、旋转、镜像)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一、图像平移
strel %用来创建形态学结构元素
translate(SE,[y x])%原结构元素SE上y和x方向平移imdilate%形态学膨胀
代码:
I=imread('Bluesky.jpg');
se=translate(strel(1),[180 190]);
B=imdilate(I,se);
figure(1);subplot(1,2,1);subimage(I);title('原图像'); subplot(1,2,2);subimage(B);title('平移后图像');
figure(2);subplot(1,2,1);imshow(I);title('原图像'); subplot(1,2,2);imshow(B);title('平移后图像');
%显然subimage()命令可以显示图像轴而imshow()不可以结果:
Figure1
Figure2:
二、图像镜像
B=imtransform(A,TFORM,method);
TFORM=makeform(transformtype,Matrix);%空间变换结构
参数transformtype指定了变换的类型,常见的’affine’为二维或多维仿射变换,包括平移、旋转、比例、拉伸和错切等。
Matrix为相应的仿射变换矩阵。
代码:
A=imread('Bluesky.jpg');
[height,width,dim]=size(A);%计算图像尺寸
tform=maketform('affine',[-1 0 0;0 1 0;width 0 1]);
%空间变换结构,其中[-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]);
%空间变换结构,其中[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('水平镜像');imwrite(B,'水平镜像.jpg');
subplot(1,3,3);imshow(C);title('垂直镜像');imwrite(B,'垂直镜像.jpg');
结果:
加上代码tightfig 后如下(白边框变小了):
Tightfig 函数代码如下:
function hfig = tightfig(hfig)
% tightfig: Alters a figure so that it has the minimum size necessary to % enclose all axes in the figure without excess space around them. %
% Note that tightfig will expand the figure to completely encompass all % axes if necessary. If any 3D axes are present which have been zoomed, % tightfig will produce an error, as these cannot easily be dealt with. %
% hfig - handle to figure, if not supplied, the current figure will be used
% instead.
原
图水平镜
像垂直镜像
if nargin == 0
hfig = gcf;
end
% There can be an issue with tightfig when the user has been modifying % the contnts manually, the code below is an attempt to resolve this, % but it has not yet been satisfactorily fixed
% origwindowstyle = get(hfig, 'WindowStyle');
set(hfig, 'WindowStyle', 'normal');
% 1 point is 0.3528 mm for future use
% get all the axes handles note this will also fetch legends and % colorbars as well
hax = findall(hfig, 'type', 'axes');
% get the original axes units, so we can change and reset these again % later
origaxunits = get(hax, 'Units');
% change the axes units to cm
set(hax, 'Units', 'centimeters');
% get various position parameters of the axes
if numel(hax) > 1
% fsize = cell2mat(get(hax, 'FontSize'));
ti = cell2mat(get(hax,'TightInset'));
pos = cell2mat(get(hax, 'Position'));
else
% fsize = get(hax, 'FontSize');
ti = get(hax,'TightInset');
pos = get(hax, 'Position');
end
% ensure very tiny border so outer box always appears
ti(ti < 0.1) = 0.15;
% we will check if any 3d axes are zoomed, to do this we will check if
% they are not being viewed in any of the 2d directions
views2d = [0,90; 0,0; 90,0];
for i = 1:numel(hax)
set(hax(i), 'LooseInset', ti(i,:));
% set(hax(i), 'LooseInset', [0,0,0,0]);
% get the current viewing angle of the axes
[az,el] = view(hax(i));
% determine if the axes are zoomed
iszoomed = strcmp(get(hax(i), 'CameraViewAngleMode'), 'manual');
% test if we are viewing in 2d mode or a 3d view
is2d = all(bsxfun(@eq, [az,el], views2d), 2);
if iszoomed && ~any(is2d)
error('TIGHTFIG:haszoomed3d', 'Cannot make figures containing zoomed 3D axes tight.')
end
end
% we will move all the axes down and to the left by the amount
% necessary to just show the bottom and leftmost axes and labels etc. moveleft = min(pos(:,1) - ti(:,1));
movedown = min(pos(:,2) - ti(:,2));
% we will also alter the height and width of the figure to just
% encompass the topmost and rightmost axes and lables
figwidth = max(pos(:,1) + pos(:,3) + ti(:,3) - moveleft);
figheight = max(pos(:,2) + pos(:,4) + ti(:,4) - movedown);
% move all the axes
for i = 1:numel(hax)
set(hax(i), 'Position', [pos(i,1:2) - [moveleft,movedown],
pos(i,3:4)]);
end
origfigunits = get(hfig, 'Units');
set(hfig, 'Units', 'centimeters');
% change the size of the figure
figpos = get(hfig, 'Position');
set(hfig, 'Position', [figpos(1), figpos(2), figwidth, figheight]);
% change the size of the paper
set(hfig, 'PaperUnits','centimeters');
set(hfig, 'PaperSize', [figwidth, figheight]);
set(hfig, 'PaperPositionMode', 'manual');
set(hfig, 'PaperPosition',[0 0 figwidth figheight]);
% reset to original units for axes and figure
if ~iscell(origaxunits)
origaxunits = {origaxunits};
end
for i = 1:numel(hax)
set(hax(i), 'Units', origaxunits{i});
end
set(hfig, 'Units', origfigunits);
% set(hfig, 'WindowStyle', origwindowstyle);
end
三、图像转置
代码段:
A=imread('Bluesky.jpg');
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('转置后');
tightfig;
imwrite(B,'转置后图像.jpg');
结果:
原
图
转置后
四、图像的中心旋转
B=imrotate(A,angle,method,’crop’);
angle为旋转角度,正值为逆时针旋转。
可选参数method 为imrotate函数指定插值方法。
‘crop’选项会裁减旋转后增大的图像,保持和原图像同样大小。
代码:
A=imread('Bluesky.jpg');
B=imrotate(A,30,'nearest','crop');%转30度
figure;imshow(B);imwrite(B,'逆时针中心旋转30度.bmp');
结果:。