-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfish_3D_origin.m
More file actions
173 lines (155 loc) · 6.35 KB
/
fish_3D_origin.m
File metadata and controls
173 lines (155 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
clc;
clear;
tic;
I0=imread('f.jpg');
%I1=extraction_fish(I0);
I2=uint8(rgb2gray(I0));
[heigh,width]=size(I2);
r=floor((min(heigh,width))/2); %原图像半径
oriwidth=2*r; %原图像宽长
oriheigh=2*r;
nwidth=4*r; %定义校正后图像大小
nheigh=4*r;
I1=uint8(zeros(nheigh,nwidth));
p1=r;
m1=r;
n1=r;
xc=r;
yc=r;
u0=r;
v0=r;
%-------------立方体顶面校正-------%
for i=1:2*r
for j=1:2*r
m=i-xc;
n=j-yc;
u=(r*m)/(sqrt(m^2+n^2+p1^2));
v=(r*n)/(sqrt(m^2+n^2+p1^2));
xft=round(u+u0);
yft=round(v+v0);
if (xft<=0)||(xft>oriwidth)||(yft<=0)||(yft>oriheigh)
I1(i,r+j,:)=0;
elseif (xft >0) && (xft <oriwidth) && (yft>0) && (yft <oriheigh)
xx= u+u0;
yy=v+v0;
a = double(uint16(xx));
b = double(uint16(yy));
x11 = double(I0(a,b)); % x11 <- origImg(a,b)
x12 = double(I0(a,b+1)); % x12 <- origImg(a,b+1)
x21 = double(I0(a+1,b)); % x21 <- origImg(a+1,b)
x22 = double(I0(a+1,b+1)); % x22 <- origImg(a+1,b+1)
I1(i,r+j) = uint8( (b+1-yy) * ((xx-a)*x21 + (a+1-xx)*x11) + (yy-b) * ((xx-a)*x22 +(a+1-xx) * x12) ); %双线性插值
else
I1(i,r+j) = I0(xft,yft); % else,apply nearest interpolate
end
end
end
%%-----------立方体左侧面校正------------------%%
for i=1:r
for j=1:2*r
p=i-xc;
m=j-yc;
u=(r*m)/(sqrt(m^2+n1^2+p^2));
v=(r*(-n1))/(sqrt(m^2+n1^2+p^2));
xft=round(u+u0);
yft=round(v+v0);
if (xft<=0)||(xft>oriwidth)||(yft<=0)||(yft>oriheigh)
I1(2*r+i,j)=0;
elseif (xft >0) && (xft <oriwidth) && (yft>0) && (yft <oriheigh)
xx= u+u0;
yy=v+v0;
a = double(uint16(xx));
b = double(uint16(yy));
x11 = double(I0(a,b)); % x11 <- origImg(a,b)
x12 = double(I0(a,b+1)); % x12 <- origImg(a,b+1)
x21 = double(I0(a+1,b)); % x21 <- origImg(a+1,b)
x22 = double(I0(a+1,b+1)); % x22 <- origImg(a+1,b+1)
I1(2*r+i,j) = uint8( (b+1-yy) * ((xx-a)*x21 + (a+1-xx)*x11) + (yy-b) * ((xx-a)*x22 +(a+1-xx) * x12) ); %双线性插值
else
I1(2*r+i,j) = I0(xft,yft); % else,apply nearest interpolate
end
end
end
%%-----------立方体右侧面校正------------------%%
for i=1:r
for j=1:2*r
p=i-xc;
m=j-yc;
u=(r*m)/(sqrt(m^2+n1^2+p^2));
v=(r*(n1))/(sqrt(m^2+n1^2+p^2));
xft=round(u+u0);
yft=round(v+v0);
if (xft<=0)||(xft>oriwidth)||(yft<=0)||(yft>oriheigh)
I1(3*r+i,j)=0;
elseif (xft >0) && (xft <oriwidth) && (yft>0) && (yft <oriheigh)
xx= u+u0;
yy=v+v0;
a = double(uint16(xx));
b = double(uint16(yy));
x11 = double(I0(a,b)); % x11 <- origImg(a,b)
x12 = double(I0(a,b+1)); % x12 <- origImg(a,b+1)
x21 = double(I0(a+1,b)); % x21 <- origImg(a+1,b)
x22 = double(I0(a+1,b+1)); % x22 <- origImg(a+1,b+1)
I1(3*r+i,j) = uint8( (b+1-yy) * ((xx-a)*x21 + (a+1-xx)*x11) + (yy-b) * ((xx-a)*x22 +(a+1-xx) * x12) ); %双线性插值
else
I1(3*r+i,j) = I0(xft,yft); % else,apply nearest interpolate
end
end
end
%%-----------立方体上侧面校正------------------%%
for i=1:r
for j=1:2*r
p=i-xc;
n=j-yc;
u=(r*(m1))/(sqrt(m1^2+n^2+p^2));
v=(r*(n))/(sqrt(m1^2+n^2+p^2));
xft=round(u+u0);
yft=round(v+v0);
if (xft<=0)||(xft>oriwidth)||(yft<=0)||(yft>oriheigh)
I1(2*r+i,2*r+j)=0;
elseif (xft >0) && (xft <oriwidth) && (yft>0) && (yft <oriheigh)
xx= u+u0;
yy=v+v0;
a = double(uint16(xx));
b = double(uint16(yy));
x11 = double(I0(a,b)); % x11 <- origImg(a,b)
x12 = double(I0(a,b+1)); % x12 <- origImg(a,b+1)
x21 = double(I0(a+1,b)); % x21 <- origImg(a+1,b)
x22 = double(I0(a+1,b+1)); % x22 <- origImg(a+1,b+1)
I1(2*r+i,2*r+j) = uint8( (b+1-yy) * ((xx-a)*x21 + (a+1-xx)*x11) + (yy-b) * ((xx-a)*x22 +(a+1-xx) * x12) ); %双线性插值
else
I1(2*r+i,2*r+j) = I0(xft,yft); % else,apply nearest interpolate
end
end
end
%%-----------立方体下侧面校正------------------%%
for i=1:r
for j=1:2*r
p=i-xc;
n=j-yc;
u=(r*(-m1))/(sqrt(m1^2+n^2+p^2));
v=(r*(n))/(sqrt(m1^2+n^2+p^2));
xft=round(u+u0);
yft=round(v+v0);
if (xft<=0)||(xft>oriwidth)||(yft<=0)||(yft>oriheigh)
I1(3*r+i,2*r+j)=0;
elseif (xft >0) && (xft <oriwidth) && (yft>0) && (yft <oriheigh)
xx= u+u0;
yy=v+v0;
a = double(uint16(xx));
b = double(uint16(yy));
x11 = double(I0(a,b)); % x11 <- origImg(a,b)
x12 = double(I0(a,b+1)); % x12 <- origImg(a,b+1)
x21 = double(I0(a+1,b)); % x21 <- origImg(a+1,b)
x22 = double(I0(a+1,b+1)); % x22 <- origImg(a+1,b+1)
I1(3*r+i,2*r+j) = uint8( (b+1-yy) * ((xx-a)*x21 + (a+1-xx)*x11) + (yy-b) * ((xx-a)*x22 +(a+1-xx) * x12) ); %双线性插值
else
I1(3*r+i,2*r+j) = I0(xft,yft); % else,apply nearest interpolate
end
end
end
figure;
subplot(1,2,1),imshow(I0);
subplot(1,2,2),imshow(I1);
toc;
time=toc;