Fast Cumulative Sum?

强颜欢笑 提交于 2021-01-28 06:09:47

问题


The menu command "Volume > Projection > Project Along Z" is really fast as compared to scripting (even with intrinsic variables). Cumulative sum (projection) of a 3D image volume of 512x512x200 in z-direction takes <0.5 sec. as compared to >8 sec. by using script. Is there a direct access this script function other than using ChooseMenuItem()?

Script example showing the difference:

// create an image of 512x512x200, assign random numbers
image img := exprsize(512, 512, 200, random());
img.SetName( "test" );
img.ShowImage();
//
number start_tick, end_tick, calc_time;

// do volume projectin with menu command : Volume>Project>Project Along Z
start_tick = GetHighResTickCount();
    ChooseMenuItem( "Volume", "Project", "Project Along Z");    // do z-projection
end_tick = GetHighResTickCount();
// calculate execution time 
calc_time = CalcHighResSecondsBetween( start_tick, end_tick );

// display result image
Image img_projZ1 := GetFrontImage();
img_projZ1.SetName( "Z-proj.#1 (" + calc_time.format("%.2fs") + ")");
img_projZ1.ShowImage();

// do volume project in z-direction (using intrinsic variable iplane)
image img_projZ2 := exprsize(512, 512, 0.0);

start_tick = GetHighResTickCount();
    img_projZ2[icol, irow, iplane] += img;  // do z-projection
end_tick = GetHighResTickCount();

// calculate execution time 
calc_time = CalcHighResSecondsBetween( start_tick, end_tick );

// display result image
img_projZ2.SetName( "Z-projection#1 (" + calc_time.format("%.2fs") + ")");
img_projZ2.ShowImage();

回答1:


Using intrinsic variables is not the fastest way to go about this in scripting. (It used to be in GMS 1 a long time ago.)

In fact, if you do it as a for loop over slices you are a bit faster than with the command menu - most likely due to the overheads of calling that command and tagging the results.

// create an image of 512x512x200, assign random numbers
number sx = 512
number sy = 512
number sz = 200

image img := RealImage("",8,sx,sy,sz)
img = random();
img.SetName( "test" );
img.ShowImage();

number start_tick, end_tick, calc_time;

// do volume projectin with menu command : Volume>Project>Project Along Z
start_tick = GetHighResTickCount();
ChooseMenuItem( "Volume", "Project", "Project Along Z");    // do z-projection
end_tick = GetHighResTickCount();

// calculate execution time 
calc_time = CalcHighResSecondsBetween( start_tick, end_tick );

// display result image
Image img_projZ1 := GetFrontImage();
img_projZ1.SetName( "Z-proj.#1 (" + calc_time.format("%.2fs") + ")");
img_projZ1.ShowImage();

// do volume project in z-direction (using intrinsic variable iplane)
image img_projZ2 := img_projZ1.ImageClone()
img_projZ2 = 0
start_tick = GetHighResTickCount();
for(number i=0; i<sz; i++)
    img_projZ2 += img.slice2(0,0,i,0,sx,1,1,sy,1)
end_tick = GetHighResTickCount();

// calculate execution time 
calc_time = CalcHighResSecondsBetween( start_tick, end_tick );

// display result image
img_projZ2.SetName( "Z-projection#1 (" + calc_time.format("%.2fs") + ")");
img_projZ2.ShowImage();

However, to answer your questions for a command: GMS 3.4 has a command called:

RealImage Project( BasicImage img, Number axis )
RealImage Project( BasicImage img, Number axis, Boolean rescale )
void Project( BasicImage img, BasicImage dst, Number axis )
void Project( BasicImage img, BasicImage dst, Number axis, Boolean rescale )

But this command is not officially documented, so it might be renamed/removed at any time.



来源:https://stackoverflow.com/questions/64103457/fast-cumulative-sum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!