function output =myscale(src,w_cof,h_cof)[h w d]=size(src);
w_cof=w_cof;
h_cof=h_cof;
image =zeros(ceil(h_cof*h),ceil(w_cof*w),d);[nh nw nd]=size(image);for y=1:nh
for x=1:nw
y0=y*h/nh;
x0=x*w/nw;
u=x0-floor(x0);
v=y0-floor(y0);if x0<1 x0=1; end
if x0>=w x0=w; end
if y0<1 y0=1; end
if y0>h y0=h;end
for dim=1:d
image(y,x,dim)=src(floor(y0),floor(x0),dim)*(1-u)*(1-v)+src(floor(y0),ceil(x0),dim)*(1-u)*v+src(ceil(y0),floor(x0),dim)*u*(1-v)+src(ceil(y0),ceil(x0),dim)*u*v;
end
end
end
output=uint8(image);
end
function [output1,output2]=mymove(src,x_dist,y_dist)[h w dim]=size(src);
h_dist=y_dist;
w_dist=x_dist;
image1=zeros(h,w,dim);
image2=zeros(h+abs(h_dist),w+abs(w_dist),dim);
T_Move=[10 w_dist;01 h_dist;001];if(x_dist <0)
x_symbol=-1;else
x_symbol=0;
end
if(y_dist <0)
y_symbol=-1;else
y_symbol=0;
end
for y0=1:h
for x0=1:w
cordinate=T_Move*[x0;y0;1];ifcordinate(2)<1cordinate(2)=1; end
ifcordinate(2)>h cordinate(2)=h; end
ifcordinate(1)<1cordinate(1)=1; end
ifcordinate(1)>w cordinate(1)=w;end
for d=1:dim
image1(floor(cordinate(2)),floor(cordinate(1)),d)=src(y0,x0,d);
end
cordinate=T_Move*[x0;y0;1];for d=1:dim
image2(floor(cordinate(2))+y_symbol*y_dist,floor(cordinate(1))+x_symbol*x_dist,d)=src(y0,x0,d);
end
end
end
output1=uint8(image1);
output2=uint8(image2);
end
src=imread('lighthouse.png');[h w dim]=size(src);
image1=zeros(h,w,dim);
image2=zeros(h,w,dim);
T_YMirror=[-10 w;010;001];
T_XMirror=[100;0-1 h;001];for y0=1:h
for x0=1:w
cordinate=T_YMirror*[x0;y0;1];for d=1:dim
image1(round(cordinate(2)),round(cordinate(1))+1,d)=src(y0,x0,d);
end
cordinate=T_XMirror*[x0;y0;1];for d=1:dim
image2(round(cordinate(2))+1,round(cordinate(1)),d)=src(y0,x0,d);
end
end
end
figure;subplot(2,2,1);imshow(src);title('原图像');subplot(2,2,2);imshow(uint8(image1));title('水平镜像');subplot(2,2,3);imshow(uint8(image2));title('垂直镜像');
图像旋转
src=imread('lighthouse.png');[h w dim]=size(src);
theta=pi/6;
T_rorate =[cos(theta)-sin(theta)0;sin(theta)cos(theta)0;001];
image1=zeros(h,w,dim);for y=1-h/2:h/2for x=1-w/2:w/2
cordinate=T_rorate*[x;y;1];cordinate(1)=cordinate(1)+w/2;cordinate(2)=cordinate(2)+h/2;if(cordinate(2)>=1&&cordinate(2)<=h &&cordinate(1)>=1&&cordinate(1)<=w)for d=1:dim
image1(round(y)+h/2,round(x)+w/2,d)=src(floor(cordinate(2)),floor(cordinate(1)),d);
end
end
end
end
nw=w*cos(theta)+h*sin(theta);
nh=w*sin(theta)+h*cos(theta);
image2=zeros(ceil(nh),ceil(nw),dim);for y=1-nh/2:nh/2for x=1-nw/2:nw/2
cordinate=T_rorate*[x;y;1];cordinate(1)=cordinate(1)+w/2;cordinate(2)=cordinate(2)+h/2;if(cordinate(2)>=1&&cordinate(2)<=h &&cordinate(1)>=1&&cordinate(1)<=w)for d=1:dim
image2(round(y+nh/2),round(x+nw/2),d)=src(floor(cordinate(2)),floor(cordinate(1)),d);
end
end
end
end
figure;subplot(2,2,1);imshow(src);subplot(2,2,2);imshow(uint8(image1));subplot(2,2,3);imshow(uint8(image2));
图像绕点旋转
function [o1,o2]=myrortate(src,angle)[h w dim]=size(src);
theta=angle;
T_rorate =[cos(theta)-sin(theta)0;sin(theta)cos(theta)0;001];
image1=zeros(h,w,dim);for y=1-h/2:h/2for x=1-w/2:w/2
cordinate=T_rorate*[x;y;1];cordinate(1)=cordinate(1)+w/2;cordinate(2)=cordinate(2)+h/2;if(cordinate(2)>=1&&cordinate(2)<=h &&cordinate(1)>=1&&cordinate(1)<=w)for d=1:dim
image1(round(y)+h/2,round(x)+w/2,d)=src(floor(cordinate(2)),floor(cordinate(1)),d);
end
end
end
end
nw=w*cos(theta)+h*sin(theta);
nh=w*sin(theta)+h*cos(theta);
image2=zeros(ceil(nh),ceil(nw),dim);for y=1-nh/2:nh/2for x=1-nw/2:nw/2
cordinate=T_rorate*[x;y;1];cordinate(1)=cordinate(1)+w/2;cordinate(2)=cordinate(2)+h/2;if(cordinate(2)>=1&&cordinate(2)<=h &&cordinate(1)>=1&&cordinate(1)<=w)for d=1:dim
image2(round(y+nh/2),round(x+nw/2),d)=src(floor(cordinate(2)),floor(cordinate(1)),d);
end
end
end
end
o1=image1;
o2=image2;
end