TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

2D Convolution (Image Filtering)

Computer Vision
Medium

Apply a CNN-style two-dimensional filter to a single-channel image. Pad every side with zeros, then slide the kernel with the supplied stride. Do not flip the kernel.

Yi,j=∑a=0Kh−1∑b=0Kw−1Xis+a,js+bpadKa,bY_{i,j}=\sum_{a=0}^{K_h-1}\sum_{b=0}^{K_w-1}X^{\mathrm{pad}}_{is+a,js+b}K_{a,b}Yi,j​=a=0∑Kh​−1​b=0∑Kw​−1​Xis+a,js+bpad​Ka,b​ Hout=⌊H+2p−Khs⌋+1H_{\mathrm{out}}=\left\lfloor\frac{H+2p-K_h}{s}\right\rfloor+1Hout​=⌊sH+2p−Kh​​⌋+1 Wout=⌊W+2p−Kws⌋+1W_{\mathrm{out}}=\left\lfloor\frac{W+2p-K_w}{s}\right\rfloor+1Wout​=⌊sW+2p−Kw​​⌋+1

Here, XXX is the image, KKK is the kernel, sss is stride, and ppp is padding. Return the output as a two-dimensional list of numbers.

Loading visualization...

Examples

Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], kernel = [[1, 0], [0, 1]], stride = 1, padding = 0

Output: [[6, 8], [12, 14]]

Explanation: Each output is the sum along the main diagonal of its two-by-two image patch.

Input: image = [[1, 2], [3, 4]], kernel = [[1, 1], [1, 1]], stride = 1, padding = 1

Output: [[1, 3, 2], [4, 10, 6], [3, 7, 4]]

Hint 1

Build a zero-filled padded grid and copy the image into its center.

Hint 2

Use four loops for output rows, output columns, kernel rows, and kernel columns.

Requirements

  • Apply zero padding on every side
  • Slide the unflipped kernel using the specified stride
  • Compute one elementwise-product sum per output position
  • Return a two-dimensional list

Constraints

  • The image and kernel are nonempty rectangular lists
  • The kernel fits the padded image
  • Stride is positive and padding is nonnegative
Try Similar Problems
Gaussian Blur KernelSobel Edge DetectionSimple Cnn LayerMax Pooling 2dMorphological Operations

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.
PrevNext

2D Convolution (Image Filtering)

Computer Vision
Medium

Apply a CNN-style two-dimensional filter to a single-channel image. Pad every side with zeros, then slide the kernel with the supplied stride. Do not flip the kernel.

Yi,j=∑a=0Kh−1∑b=0Kw−1Xis+a,js+bpadKa,bY_{i,j}=\sum_{a=0}^{K_h-1}\sum_{b=0}^{K_w-1}X^{\mathrm{pad}}_{is+a,js+b}K_{a,b}Yi,j​=a=0∑Kh​−1​b=0∑Kw​−1​Xis+a,js+bpad​Ka,b​ Hout=⌊H+2p−Khs⌋+1H_{\mathrm{out}}=\left\lfloor\frac{H+2p-K_h}{s}\right\rfloor+1Hout​=⌊sH+2p−Kh​​⌋+1 Wout=⌊W+2p−Kws⌋+1W_{\mathrm{out}}=\left\lfloor\frac{W+2p-K_w}{s}\right\rfloor+1Wout​=⌊sW+2p−Kw​​⌋+1

Here, XXX is the image, KKK is the kernel, sss is stride, and ppp is padding. Return the output as a two-dimensional list of numbers.

Loading visualization...

Examples

Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], kernel = [[1, 0], [0, 1]], stride = 1, padding = 0

Output: [[6, 8], [12, 14]]

Explanation: Each output is the sum along the main diagonal of its two-by-two image patch.

Input: image = [[1, 2], [3, 4]], kernel = [[1, 1], [1, 1]], stride = 1, padding = 1

Output: [[1, 3, 2], [4, 10, 6], [3, 7, 4]]

Hint 1

Build a zero-filled padded grid and copy the image into its center.

Hint 2

Use four loops for output rows, output columns, kernel rows, and kernel columns.

Requirements

  • Apply zero padding on every side
  • Slide the unflipped kernel using the specified stride
  • Compute one elementwise-product sum per output position
  • Return a two-dimensional list

Constraints

  • The image and kernel are nonempty rectangular lists
  • The kernel fits the padded image
  • Stride is positive and padding is nonnegative
Try Similar Problems
Gaussian Blur KernelSobel Edge DetectionSimple Cnn LayerMax Pooling 2dMorphological Operations

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

Accepts: number

Accepts: number

You must run your code first.