在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 close all;
2 clc;
3 H=1; %索引pix中第一个元素,即高度
4 W=2; %索引pix中第二个元素,即宽度
5 left_right=0.3; %抬起左边或右边时值为0-1之间,不抬起时为0
6 up_down=0; %抬起上边或下边时值为0-1之间,不抬起时为0
7
8 img=imread('lena.jpg'); %这里v为原图像的高度,u为原图像的宽度
9 imshow(img); %这里y为变换后图像的高度,x为变换后图像的宽度
10 %img=flipud(img); %注释掉为抬起下边,没注释掉为抬起上边
11 %img=fliplr(img); %注释掉为抬起右边,没注释掉为抬起左边
12 [v u]=size(img);
13
14
15 a=1;b=up_down;c=0;
16 d=left_right;e=1;f=0;
17 g=up_down/v;h=left_right/u;i=1;
18 rot=[a b c;d e f;g h i];
19
20 pix1=[1 1 1]*rot./(g+h+i); %变换后图像左上点的坐标
21 pix2=[1 u 1]*rot./(g*v+h+i); %变换后图像右上点的坐标
22 pix3=[v 1 1]*rot./(g+h*u+i); %变换后图像左下点的坐标
23 pix4=[v u 1]*rot./(g*v+h*u+i); %变换后图像右下点的坐标
24
25 height=round(max([abs(pix1(H)-pix3(H))+0.5 abs(pix2(H)-pix3(H))+0.5 ...
26 abs(pix1(H)-pix4(H))+0.5 abs(pix2(H)-pix4(H))+0.5])); %变换后图像的高度
27
28 width=round(max([abs(pix1(W)-pix2(W))+0.5 abs(pix3(W)-pix2(W))+0.5 ...
29 abs(pix1(W)-pix4(W))+0.5 abs(pix3(W)-pix4(W))+0.5])); %变换后图像的宽度
30 imgn=zeros(height,width);
31
32 delta_y=abs(min([pix1(H)-0.5 pix2(H)-0.5 pix3(H)-0.5 pix4(H)-0.5])); %取得y方向的负轴超出的偏移量
33 delta_x=abs(min([pix1(W)-0.5 pix2(W)-0.5 pix3(W)-0.5 pix4(W)-0.5])); %取得x方向的负轴超出的偏移量
34
35 for y=1-floor(delta_y):height-floor(delta_y)
36 for x=1-floor(delta_x):width-floor(delta_x)
37 pix=[y x 1]/rot*(g*y+h*x+i); %用变换后图像的点的坐标去寻找原图像点的坐标,
38 %否则有些变换后的图像的像素点无法完全填充
39 if pix(H)>=0.5 && pix(W)>=0.5 && pix(H)<=v && pix(W)<=u
40 imgn(y+floor(delta_y),x+floor(delta_x))=img(round(pix(H)),round(pix(W)));
41 end
42
43 end
44 end
45 figure,imshow(uint8(imgn));
46
47 %%二次变换,暂时只想起来这种笨方法,一次变换好像没法进行透视变换
48 img=imgn;
49 [v u]=size(img);
50 a=1;b=-b/2;c=0;
51 d=-d/2;e=1;f=0;
52 g=0;h=0;i=1;
53 rot=[a b c;d e f;g h i];
54
55 pix1=[1 1 1]*rot./(g+h+i); %变换后图像左上点的坐标
56 pix2=[1 u 1]*rot./(g*v+h+i); %变换后图像右上点的坐标
57 pix3=[v 1 1]*rot./(g+h*u+i); %变换后图像左下点的坐标
58 pix4=[v u 1]*rot./(g*v+h*u+i); %变换后图像右下点的坐标
59
60 height=round(max([abs(pix1(H)-pix3(H))+0.5 abs(pix2(H)-pix3(H))+0.5 ...
61 abs(pix1(H)-pix4(H))+0.5 abs(pix2(H)-pix4(H))+0.5])); %变换后图像的高度
62
63 width=round(max([abs(pix1(W)-pix2(W))+0.5 abs(pix3(W)-pix2(W))+0.5 ...
64 abs(pix1(W)-pix4(W))+0.5 abs(pix3(W)-pix4(W))+0.5])); %变换后图像的宽度
65 imgn=zeros(height,width);
66
67 delta_y=abs(min([pix1(H)-0.5 pix2(H)-0.5 pix3(H)-0.5 pix4(H)-0.5])); %取得y方向的负轴超出的偏移量
68 delta_x=abs(min([pix1(W)-0.5 pix2(W)-0.5 pix3(W)-0.5 pix4(W)-0.5])); %取得x方向的负轴超出的偏移量
69
70 for y=1-floor(delta_y):height-floor(delta_y)
71 for x=1-floor(delta_x):width-floor(delta_x)
72 pix=[y x 1]/rot*(g*y+h*x+i); %用变换后图像的点的坐标去寻找原图像点的坐标,
73 %否则有些变换后的图像的像素点无法完全填充
74 if pix(H)>=0.5 && pix(W)>=0.5 && pix(H)<=v && pix(W)<=u
75 imgn(y+floor(delta_y),x+floor(delta_x))=img(round(pix(H)),round(pix(W)));
76 end
77
78 end
79 end
80 %imgn=flipud(imgn); %注释掉为抬起下边,没注释掉为抬起上边
81 %imgn=fliplr(imgn); %注释掉为抬起右边,没注释掉为抬起左边
82 figure,imshow(uint8(imgn));
图片福利:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论