plenoptic.process.correlate_downsample#

plenoptic.process.correlate_downsample(image, filt, padding_mode='reflect')[source]#

Correlate with a filter and downsample by a factor of 2.

This operation allows one to downsample in an alias-resistant manner, removing the high frequencies that would result in aliasing in a smaller image.

Parameters:
  • image (Tensor) – Image, or batch of images, of shape (batch, channel, height, width). Batches and channels are handled independently.

  • filt (Tensor) – 2D tensor defining the filter to correlate with the input image.

  • padding_mode (Literal['constant', 'reflect', 'replicate', 'circular'] (default: 'reflect')) – How to pad the image, so that we return an image of the appropriate size. The option "constant" means padding with zeros.

Return type:

Tensor

Returns:

downsampled_image – The downsampled image.

Raises:

ValueError – If filt or image has the wrong number of dimensions.

See also

blur_downsample

Perform this operation a user-specified number of times using a named filter.

upsample_convolve

Perform the inverse operation, upsampling and convolving with a filter.

Examples

>>> import plenoptic as po
>>> import torch
>>> img = po.data.einstein()
>>> # 2x2 averaging filter
>>> filt = torch.ones(2, 2) / 4.0
>>> downsampled = po.process.correlate_downsample(img, filt)
>>> downsampled.shape
torch.Size([1, 1, 128, 128])
>>> po.plot.imshow([img, downsampled], title=["image", "downsampled"])
<PyrFigure...>

(png, hires.png, pdf)

../../_images/plenoptic-process-correlate_downsample-1.png

Note that the dimensions have changed.

This function always returns an image whose height and width are half that of the input (rounded up). When convolving an image with a filter, the filter must be centered on each output pixel. For pixels near the image boundary, the filter extends outside the image boundary and thus we need to pad the input with extra pixels. The padding_mode argument determines how to do so (using same_padding):

  • reflect: mirror the image at boundaries

  • constant: pad with zeroes

  • replicate: repeat edge pixel values

  • circular: wrap the image around

>>> # Large 50x50 averaging filter to make padding effects visible
>>> filt = torch.ones(50, 50) / (50 * 50)
>>> constant = po.process.correlate_downsample(img, filt, padding_mode="constant")
>>> reflect = po.process.correlate_downsample(img, filt, padding_mode="reflect")
>>> replicate = po.process.correlate_downsample(
...     img, filt, padding_mode="replicate"
... )
>>> circular = po.process.correlate_downsample(img, filt, padding_mode="circular")
>>> po.plot.imshow(
...     [reflect, constant, replicate, circular],
...     title=[
...         "reflect padding",
...         "constant (zero) padding",
...         "replicate padding",
...         "circular padding",
...     ],
...     zoom=2,
... )
<PyrFigure...>

(png, hires.png, pdf)

../../_images/plenoptic-process-correlate_downsample-2.png