使用ImageJ板加载工具对图像进行高斯下采样。
动机
图像的声音下采样需要消除结果中图像采样频率一半的图像频率(请参阅Nyquist–Shannon sampling theorem)。用于此目的的专用工具是Gaussian convolution。
下载
从此处的git存储库获取快照:downsample_.js。
文档
该脚本计算给定目标宽度或高度所需的高斯平滑,平滑图像并重新采样。目标尺寸必须小于源图像尺寸。
另外,您可以定义源图像和目标图像的“固有”高斯清晰。最佳采样器由 sigma=0.5 确定。如果您的源图像已经模糊,您可以设置更高的源图像清晰明确的结果。将目标 sigma 设置为小于 0.5 的值使得结果清晰度更佳,因此最终会出现混叠。
该脚本由Stephan Saalfeld维护。
示例
一张图片胜过一千个文字,所以这里有一个例子。您会看到 2,048×2,048 个像素的电子显微照片,其采样率降低至 100×100 个像素。为了更好地说明,示例以 200% 的比例显示。

ImageJ interpolated scaling

Gaussian downsampling with target sigma=0.25

Gaussian downsampling with target sigma=0.5
代码
/**
* Gaussian downsampling of an image with ImageJ on-board tools.
*
* Motivation:
* Sound downsampling of an image requires the elimination of image frequencies
* higher than half the sampling frequency in the result image (see the
* Nyquist-Shannon sampling theorem). The exclusive tool for this is Gaussian
* convolution.
*
* This script calculates the required Gaussian kernel for a given target size,
* smoothes the image and resamples it.
*
* Furthermore, you can define the "intrinsic" Gaussian kernel of the source and
* target images. An optimal sampler is identified by sigma=0.5. If your
* source image was blurred already, you may set a higher source sigma for a
* sharper result. Setting target sigma to values smaller than 0.5 makes the
* result appear sharper and therefore eventually aliased.
*/
var imp = WindowManager.getCurrentImage();
var width = 0;
var height = 0;
var sourceSigma = 0.5;
var targetSigma = 0.5;
var widthField;
var heightField;
var fieldWithFocus;
var textListener = new java.awt.event.TextListener(
{
textValueChanged : function( e )
{
var source = e.getSource();
var newWidth = Math.round( widthField.getText() );
var newHeight = Math.round( heightField.getText() );
if ( source == widthField && fieldWithFocus == widthField && newWidth )
{
newHeight = Math.round( newWidth * imp.getHeight() / imp.getWidth() );
heightField.setText( newHeight );
}
else if ( source == heightField && fieldWithFocus == heightField && newHeight )
{
newWidth = Math.round( newHeight * imp.getWidth() / imp.getHeight() );
widthField.setText( newWidth );
}
}
} );
var focusListener = new java.awt.event.FocusListener(
{
focusGained : function ( e )
{
fieldWithFocus = e.getSource();
},
focusLost : function( e ){}
} );
if ( imp )
{
width = imp.getWidth();
height = imp.getHeight();
gd = new GenericDialog( "Downsample" );
gd.addNumericField( "width :", width, 0 );
gd.addNumericField( "height :", height, 0 );
gd.addNumericField( "source sigma :", sourceSigma, 2 );
gd.addNumericField( "target sigma :", targetSigma, 2 );
gd.addCheckbox( "keep source image", true );
var fields = gd.getNumericFields();
widthField = fields.get( 0 );
heightField = fields.get( 1 );
fieldWithFocus = widthField;
widthField.addFocusListener( focusListener );
widthField.addTextListener( textListener );
heightField.addFocusListener( focusListener );
heightField.addTextListener( textListener );
gd.showDialog();
if ( gd.wasOKed() )
{
width = gd.getNextNumber();
height = gd.getNextNumber();
sourceSigma = gd.getNextNumber();
targetSigma = gd.getNextNumber();
keepSource = gd.getNextBoolean();
if ( width <= imp.getWidth() )
{
var s;
if ( fieldWithFocus == widthField )
s = targetSigma * imp.getWidth() / width;
else
s = targetSigma * imp.getHeight() / height;
if ( keepSource )
IJ.run( "Duplicate...", "title=" + imp.getTitle() + " duplicate" );
IJ.run( "Gaussian Blur...", "sigma=" + Math.sqrt( s * s - sourceSigma * sourceSigma ) + " stack" );
IJ.run( "Scale...", "x=- y=- width=" + width + " height=" + height + " process title=- interpolation=None" );
IJ.run( "Canvas Size...", "width=" + width + " height=" + height + " position=Center" );
}
else
IJ.showMessage( "You try to upsample the image. You need an interpolator for that not a downsampler." );
}
}
else
IJ.showMessage( "You should have at least one image open." );