2 # mrcImageHornSchunck : $Revision$
5 # Usage : mrcImageHornSchunck
15 #define GLOBAL_DECLARATION
16 #include "../inc/config.h"
21 #include "mrcImageHornSchunck.h"
24 /////////////////////////コード描き始め////////////////////////
27 //#define WIDTH 128 // ボリュームの幅
28 //#define HEIGHT 128 // ボリュームの高さ
29 //#define DEPTH 128 // ボリュームの奥行き
30 //#define SCALE_FACTOR 1.0 // スケールファクタ
31 //#define MAX_ITERATIONS 10 // 反復回数
32 //#define ALPHA 1.0 // パラメータ α
36 int main(int argc, char* argv[])
38 mrcImageHornSchunckInfo info;
66 argCheck(&info, argc, argv);
70 DEBUGPRINT("Program Start\n");
71 mrcFileRead(&in1, info.In1, "in main", 0);
72 mrcFileRead(&in2, info.In2, "in main", 0);
74 mrcFileRead(&in3, info.In3, "in main", 0);
78 outIx.Header = in1.Header;
79 outIy.Header = in1.Header;
80 outIz.Header = in1.Header;
81 outIt.Header = in1.Header;
82 outU.Header = in1.Header;
83 outV.Header = in1.Header;
84 outW.Header = in1.Header;
85 Size.Header = in1.Header;
86 Size1.Header = in1.Header;
87 Size2.Header = in1.Header;
88 Size3.Header = in1.Header;
89 Size4.Header = in1.Header;
90 Size5.Header = in1.Header;
91 Size6.Header = in1.Header;
92 Size7.Header = in1.Header;
93 Size8.Header = in1.Header;
94 Size9.Header = in1.Header;
95 Size10.Header = in1.Header;
97 //Conv.Header = in1.Header;
100 mrcInit(&outIx, NULL);
101 mrcInit(&outIy, NULL);
102 mrcInit(&outIz, NULL);
103 mrcInit(&outIt, NULL);
104 mrcInit(&outU, NULL);
105 mrcInit(&outV, NULL);
106 mrcInit(&outW, NULL);
107 mrcInit(&Size, NULL);
108 mrcInit(&Size1, NULL);
109 mrcInit(&Size2, NULL);
110 mrcInit(&Size3, NULL);
111 mrcInit(&Size4, NULL);
112 mrcInit(&Size5, NULL);
113 mrcInit(&Size6, NULL);
114 mrcInit(&Size7, NULL);
115 mrcInit(&Size8, NULL);
116 mrcInit(&Size9, NULL);
117 mrcInit(&Size10, NULL);
119 //mrcInit(&Conv, NULL);
122 double Ix, Iy, Iz, It;
123 Ix = Iy = Iz = It = 0;
125 // Ix,Iy,Izの計算の一次データ保管場所としての変数
126 double data1, data2, data3, data4, data5, data6, data7, data8, data9, data10, data11, data12, data13, data14, data15, data16;
128 data1 = data2 = data3 = data4 = data5 = data6 = data7 = data8 = data9 = data10 = data11 = data12 = data13 = data14 = data15 = data16 = 0;
131 double u_ave, v_ave, w_ave;
133 //オプティカルフローの一回前の値の一時的な格納場所
134 //double size_before = 0;
148 // inputfileがある場合勾配の計算をする(Ix,Iy,Iz,Itの)
149 for (x = 1; x < in1.HeaderN.x - 1; x++) {
150 for (y = 1; y < in1.HeaderN.y - 1; y++) {
151 for(z =1; z < in1.HeaderN.z - 1; z++) {
153 // オプテカルフローIx,Iy,Iz,Itの計算(平滑性制約に倣って)
155 mrcPixelDataGet(&in1, x , y , z , &data1, mrcPixelRePart, mrcPixelHowNearest);
156 mrcPixelDataGet(&in1, x+1, y , z , &data2, mrcPixelRePart, mrcPixelHowNearest);
157 mrcPixelDataGet(&in1, x , y+1, z , &data3, mrcPixelRePart, mrcPixelHowNearest);
158 mrcPixelDataGet(&in1, x+1, y+1, z , &data4, mrcPixelRePart, mrcPixelHowNearest);
159 mrcPixelDataGet(&in1, x , y , z+1, &data5, mrcPixelRePart, mrcPixelHowNearest);
160 mrcPixelDataGet(&in1, x+1, y , z+1, &data6, mrcPixelRePart, mrcPixelHowNearest);
161 mrcPixelDataGet(&in1, x , y+1, z+1, &data7, mrcPixelRePart, mrcPixelHowNearest);
162 mrcPixelDataGet(&in1, x+1, y+1, z+1, &data8, mrcPixelRePart, mrcPixelHowNearest);
164 mrcPixelDataGet(&in2, x , y , z , &data9, mrcPixelRePart, mrcPixelHowNearest);
165 mrcPixelDataGet(&in2, x+1, y , z , &data10, mrcPixelRePart, mrcPixelHowNearest);
166 mrcPixelDataGet(&in2, x , y+1, z , &data11, mrcPixelRePart, mrcPixelHowNearest);
167 mrcPixelDataGet(&in2, x+1, y+1, z , &data12, mrcPixelRePart, mrcPixelHowNearest);
168 mrcPixelDataGet(&in2, x , y , z+1, &data13, mrcPixelRePart, mrcPixelHowNearest);
169 mrcPixelDataGet(&in2, x+1, y , z+1, &data14, mrcPixelRePart, mrcPixelHowNearest);
170 mrcPixelDataGet(&in2, x , y+1, z+1, &data15, mrcPixelRePart, mrcPixelHowNearest);
171 mrcPixelDataGet(&in2, x+1, y+1, z+1, &data16, mrcPixelRePart, mrcPixelHowNearest);
173 Ix = 0.125 * ((data2-data1)+(data4-data3)+(data6-data5)+(data8-data7)+(data10-data9)+(data12-data11)+(data14-data13)+(data16-data15));
176 Iy = 0.125 * ((data3-data1)+(data4-data2)+(data7-data5)+(data8-data6)+(data11-data9)+(data12-data10)+(data15-data13)+(data16-data14));
179 Iz = 0.125 * ((data5-data1)+(data6-data2)+(data7-data3)+(data8-data4)+(data13-data9)+(data14-data10)+(data15-data11)+(data16-data12));
183 mrcPixelDataGet(&in1, x , y , z , &data1, mrcPixelRePart, mrcPixelHowNearest);
184 mrcPixelDataGet(&in2, x , y , z , &data2, mrcPixelRePart, mrcPixelHowNearest);
186 mrcPixelDataGet(&in1, x+1, y , z , &data3, mrcPixelRePart, mrcPixelHowNearest);
187 mrcPixelDataGet(&in2, x+1, y , z , &data4, mrcPixelRePart, mrcPixelHowNearest);
189 mrcPixelDataGet(&in1, x , y+1, z , &data5, mrcPixelRePart, mrcPixelHowNearest);
190 mrcPixelDataGet(&in2, x , y+1, z , &data6, mrcPixelRePart, mrcPixelHowNearest);
192 mrcPixelDataGet(&in1, x+1, y+1, z , &data7, mrcPixelRePart, mrcPixelHowNearest);
193 mrcPixelDataGet(&in2, x+1, y+1, z , &data8, mrcPixelRePart, mrcPixelHowNearest);
196 mrcPixelDataGet(&in1, x , y , z+1, &data9, mrcPixelRePart, mrcPixelHowNearest);
197 mrcPixelDataGet(&in2, x , y , z+1, &data10, mrcPixelRePart, mrcPixelHowNearest);
199 mrcPixelDataGet(&in1, x+1, y , z+1, &data11, mrcPixelRePart, mrcPixelHowNearest);
200 mrcPixelDataGet(&in2, x+1, y , z+1, &data12, mrcPixelRePart, mrcPixelHowNearest);
202 mrcPixelDataGet(&in1, x , y+1, z+1, &data13, mrcPixelRePart, mrcPixelHowNearest);
203 mrcPixelDataGet(&in2, x , y+1, z+1, &data14, mrcPixelRePart, mrcPixelHowNearest);
205 mrcPixelDataGet(&in1, x+1, y+1, z+1, &data15, mrcPixelRePart, mrcPixelHowNearest);
206 mrcPixelDataGet(&in2, x+1, y+1, z+1, &data16, mrcPixelRePart, mrcPixelHowNearest);
209 It = 0.125 * ((data2-data1)+(data4-data3)+(data6-data5)+(data8-data7)+(data10-data9)+(data12-data11)+(data14-data13)+(data16-data15));
212 //DEBUGPRINT("Program Start %f", &x);
213 mrcPixelDataSet(&outIx, x, y, z, Ix, mrcPixelRePart);
214 mrcPixelDataSet(&outIy, x, y, z, Iy, mrcPixelRePart);
215 mrcPixelDataSet(&outIz, x, y, z, Iz, mrcPixelRePart);
216 mrcPixelDataSet(&outIt, x, y, z, It, mrcPixelRePart);
222 //outputfileがオプション指定されている場合、情報を書き込む
224 mrcFileWrite(&outIx, info.OutIx, "in main", 0);
225 mrcFileWrite(&outIy, info.OutIy, "in main", 0);
226 mrcFileWrite(&outIz, info.OutIz, "in main", 0);
227 mrcFileWrite(&outIt, info.OutIt, "in main", 0);
232 // MAX_ITERATIONS従って反復して計算を行う
233 for(l = 1; l <= info.MAX_ITERATIONS; l++) {
234 for(x = 1; x < in1.HeaderN.x - 1; x++){
235 for(y = 1; y < in1.HeaderN.y - 1; y++){
236 for(z = 1; z < in1.HeaderN.z - 1; z++){
239 mrcPixelDataGet(&outIx, x, y, z, &Ix, mrcPixelRePart, mrcPixelHowNearest);
240 mrcPixelDataGet(&outIx, x, y, z, &Iy, mrcPixelRePart, mrcPixelHowNearest);
241 mrcPixelDataGet(&outIz, x, y, z, &Iz, mrcPixelRePart, mrcPixelHowNearest);
242 mrcPixelDataGet(&outIt, x, y, z, &It, mrcPixelRePart, mrcPixelHowNearest);
244 //一回目の反復におけるu,v,w,の平均値の計算
245 mrcPixelDataGet(&outU, x+1, y , z , &data1, mrcPixelRePart, mrcPixelHowNearest);
246 mrcPixelDataGet(&outU, x-1, y , z , &data2, mrcPixelRePart, mrcPixelHowNearest);
247 mrcPixelDataGet(&outU, x , y+1, z , &data3, mrcPixelRePart, mrcPixelHowNearest);
248 mrcPixelDataGet(&outU, x , y-1, z , &data4, mrcPixelRePart, mrcPixelHowNearest);
249 mrcPixelDataGet(&outU, x , y , z+1, &data5, mrcPixelRePart, mrcPixelHowNearest);
250 mrcPixelDataGet(&outU, x , y , z-1, &data6, mrcPixelRePart, mrcPixelHowNearest);
252 u_ave = (data1 + data2 + data3 + data4 + data5 + data6) / 6;
254 mrcPixelDataGet(&outV, x+1, y , z , &data1, mrcPixelRePart, mrcPixelHowNearest);
255 mrcPixelDataGet(&outV, x-1, y , z , &data2, mrcPixelRePart, mrcPixelHowNearest);
256 mrcPixelDataGet(&outV, x , y+1, z , &data3, mrcPixelRePart, mrcPixelHowNearest);
257 mrcPixelDataGet(&outV, x , y-1, z , &data4, mrcPixelRePart, mrcPixelHowNearest);
258 mrcPixelDataGet(&outV, x , y , z+1, &data5, mrcPixelRePart, mrcPixelHowNearest);
259 mrcPixelDataGet(&outV, x , y , z-1, &data6, mrcPixelRePart, mrcPixelHowNearest);
261 v_ave = (data1 + data2 + data3 + data4 + data5 + data6) / 6;
263 mrcPixelDataGet(&outW, x+1, y , z , &data1, mrcPixelRePart, mrcPixelHowNearest);
264 mrcPixelDataGet(&outW, x-1, y , z , &data2, mrcPixelRePart, mrcPixelHowNearest);
265 mrcPixelDataGet(&outW, x , y+1, z , &data3, mrcPixelRePart, mrcPixelHowNearest);
266 mrcPixelDataGet(&outW, x , y-1, z , &data4, mrcPixelRePart, mrcPixelHowNearest);
267 mrcPixelDataGet(&outW, x , y , z+1, &data5, mrcPixelRePart, mrcPixelHowNearest);
268 mrcPixelDataGet(&outW, x , y , z-1, &data6, mrcPixelRePart, mrcPixelHowNearest);
270 w_ave = (data1 + data2 + data3 + data4 + data5 + data6) / 6;
273 //u = u_ave - (Ix * (Ix * u_ave + Iy * v_ave + Iz * w_ave + It)) / ((1 / info.ALPHA) + Ix * Ix + Iy * Iy + Iz * Iz);
274 //v = v_ave - (Iy * (Ix * u_ave + Iy * v_ave + Iz * w_ave + It)) / ((1 / info.ALPHA) + Ix * Ix + Iy * Iy + Iz * Iz);
275 //w = w_ave - (Iz * (Ix * u_ave + Iy * v_ave + Iz * w_ave + It)) / ((1 / info.ALPHA) + Ix * Ix + Iy * Iy + Iz * Iz);
277 u = u_ave - (Ix * (Ix * u_ave + Iy * v_ave + Iz * w_ave + It)) / (info.ALPHA * info.ALPHA + Ix * Ix + Iy * Iy + Iz * Iz);
278 v = v_ave - (Iy * (Ix * u_ave + Iy * v_ave + Iz * w_ave + It)) / (info.ALPHA * info.ALPHA + Ix * Ix + Iy * Iy + Iz * Iz);
279 w = w_ave - (Iz * (Ix * u_ave + Iy * v_ave + Iz * w_ave + It)) / (info.ALPHA * info.ALPHA + Ix * Ix + Iy * Iy + Iz * Iz);
281 size = sqrt(u*u + v*v + w*w);
283 //オプティカルフロー の差を出してどのぐらい収束するのか知るために...
286 mrcPixelDataSet(&Size1, x, y, z, size, mrcPixelRePart);
289 mrcPixelDataSet(&Size2, x, y, z, size, mrcPixelRePart);
292 mrcPixelDataSet(&Size3, x, y, z, size, mrcPixelRePart);
295 mrcPixelDataSet(&Size4, x, y, z, size, mrcPixelRePart);
298 mrcPixelDataSet(&Size5, x, y, z, size, mrcPixelRePart);
301 mrcPixelDataSet(&Size6, x, y, z, size, mrcPixelRePart);
304 mrcPixelDataSet(&Size7, x, y, z, size, mrcPixelRePart);
307 mrcPixelDataSet(&Size8, x, y, z, size, mrcPixelRePart);
310 mrcPixelDataSet(&Size9, x, y, z, size, mrcPixelRePart);
313 mrcPixelDataSet(&Size10, x, y, z, size, mrcPixelRePart);
316 mrcPixelDataSet(&outU, x, y, z, u, mrcPixelRePart);
317 mrcPixelDataSet(&outV, x, y, z, v, mrcPixelRePart);
318 mrcPixelDataSet(&outW, x, y, z, w, mrcPixelRePart);
319 mrcPixelDataSet(&Size, x, y, z, size, mrcPixelRePart);
320 //printf("反復数%d回目\n", l);
324 printf("反復数%d回目\n", l);
329 mrcFileWrite(&outU, info.outU, "in main", 0);
330 mrcFileWrite(&outV, info.outV, "in main", 0);
331 mrcFileWrite(&outW, info.outW, "in main", 0);
334 mrcFileWrite(&Size, info.Size, "in main", 0);
337 mrcFileWrite(&Size1, info.Size1, "in main", 0);
338 mrcFileWrite(&Size2, info.Size2, "in main", 0);
339 mrcFileWrite(&Size3, info.Size3, "in main", 0);
340 mrcFileWrite(&Size4, info.Size4, "in main", 0);
341 mrcFileWrite(&Size5, info.Size5, "in main", 0);
342 mrcFileWrite(&Size6, info.Size6, "in main", 0);
343 mrcFileWrite(&Size7, info.Size7, "in main", 0);
344 mrcFileWrite(&Size8, info.Size8, "in main", 0);
345 mrcFileWrite(&Size9, info.Size9, "in main", 0);
346 mrcFileWrite(&Size10, info.Size10, "in main", 0);
351 mrcFileWrite(&Conv, info.Conv, "in main", 0);
362 fprintf(stderr, "----- Additional Usage -----\n");