当我们处理图像时,我们可以通过“处理 - 缩放 - 尺寸 - 宽度和高度,然后在DM软件中更改像素的宽度或高度数”来调整图像的大小。
当我们调整图像大小时,我们还有“约束比例”选项。
如何通过脚本实现这一目标?
答案 0 :(得分:1)
好问题。
您需要几个命令。 ImageResize()
改变图像的物理尺寸(即像素尺寸),同时保持元数据(标签)并同时改变校准,使得整个视场在校准单元中保持相同。但是,像素值重置为0,需要在第二步重新计算。
命令warp()
用于任何具有强度值双线性插值的映射,因此您可以使用该值进行缩放(加插值)。
如果您想使用“最近邻居”插值(即复制像素值),您可以通过简单的数据复制使用slice2()
命令进行子采样或仅使用[]表示法来轻松实现此目的像素索引。
由于你要求的东西是脚本中的“基本需求”,它的答案实际上已包含在后来的GMS版本的F1帮助文档的“示例”部分中,所以我只是复制粘贴这里的脚本:
image in, out1, out2
if ( !GetFrontImage( in ) )
Throw( "No image loaded." )
number sx, sy
GetSize( in, sx, sy )
number f = 1.8 // scaling factor
// Variant 1, bi-linear interpolation
out1 := ImageClone( in )
ImageResize( out1, 2, sx * f, sy * f )
out1 = Warp( in, icol / f, irow / f )
SetName( out1, GetName( in ) + " bilinear" )
ShowImage( out1 )
// Variant 2, nearest-neighbor interpolation / sampling
out2 := ImageClone( in )
ImageResize( out2, 2, sx * f, sy * f )
out2 = in[ icol / f, irow / f ]
SetName( out2, GetName( in ) + " nn" )
ShowImage( out2 )
// Note: ImageResize() sets all values to zero and
// adjusts spatial calibration to keep same FOV as before
现在,如果你想限制宽高比,那么你需要通过确保在X和Y中使用相同的采样因子来自己编写脚本。如果你想模仿'用户输入决赛大小'你会做这样的事情:
image in
if ( !GetFrontImage( in ) )
Throw( "No image loaded." )
number sx = ImageGetDimensionSize( in, 0 )
number sy = ImageGetDimensionSize( in, 1 )
string msg = "Please enter wanted X size."
msg += "\n(Currently: " + sx + " pixels)"
number sx_new
if ( !GetNumber( msg, sx, sx_new) )
exit( 0 )
number f = sx_new/sx
number sy_new = trunc(sx * f)
Result( "\n New Image size: " + sx_new + " x " + sy_new )
image out1 := ImageClone( in )
ImageResize( out1, 2, sx * f, sy * f )
out1 = Warp( in, icol / f, irow / f )
SetName( out1, GetName( in ) + " scaled" )
ShowImage( out1 )