如何通过 DM 脚本调整图像大小?

How to resize an image by DM scripting?

如何通过 DM 脚本调整图片大小?

我们在处理图片的时候,可以在DM软件中通过“处理-缩放-尺寸-宽高,然后改变宽高的像素数”来调整图片的大小。

我们在调整图像大小时也有“限制比例”选项。

如何通过脚本实现?

好问题。

您需要几个命令。 ImageResize() 更改图像的物理尺寸(即像素尺寸),同时保留元数据(标签)并同时更改校准,以便整体视野在校准单位中保持不变。但是,像素值重置为 0,需要在第二步中重新计算。

命令 warp() 用于任何具有强度值双线性插值的映射,因此您可以使用该命令进行缩放(加上插值)。

如果您想使用“最近邻”插值(即复制像素值),您可以通过使用 slice2() 命令进行子采样或仅 [ ] 像素索引符号。

由于您要求的东西是脚本编写中的“基本需求”,因此它的答案实际上已包含在以后 GMS 版本的 F1 帮助文档的“示例”部分中,所以我只是复制- 在此处粘贴脚本:

示例 3:使用强度插值调整大小

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 中使用相同的采样因子。如果你想模仿 'User enters finals size' 你会这样做:

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 )