Skip to main content
1 of 2
Seanny123
  • 1.6k
  • 3
  • 19
  • 37

Convolution to reduce popcorn noise in black and white video

I have a black & white video as a tensor with the shape [Time, Width, Height] with popcorn noise and I would like to reduce the noise by naively convolving along the time dimension.

Using a Pytorch forum post as a starting point, I wrote the following function:

import torch
from torch import nn

# dummy input tensor
input_tensor = torch.rand(100, 32, 32)

with torch.no_grad():
    t = input_tensor.view(1, *input_tensor.shape).transpose(1, 2)   # swap seq and channels dim -> SHAPE: [C, T, H, W]

    # define the low-pass filter kernel
    kernel_size = 5
    kernel = torch.ones(kernel_size) / kernel_size

    # convolve along the time dimension
    conv_temp = nn.Conv3d(1, 1, kernel_size=(kernel_size, 1, 1), bias=False)
    conv_temp.weight.data = kernel.view(1, 1, kernel_size, 1, 1)
    output_tensor = conv_temp(t).squeeze(0).transpose(0, 1)

Is this the right approach? Am I using the API correctly?

Seanny123
  • 1.6k
  • 3
  • 19
  • 37