Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Apply 2D Image Convolution

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Apply 2D Image Convolution. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given a 2D grayscale image image as a list of lists of integers and a square filter kernel as a list of lists of integers, implement a function that returns a new 2D image produced by applying the kernel to every pixel using convolution-like neighborhood summation. Assume zero-padding outside image boundaries, and clamp each output value to the range [0, 255].

Constraints

  • 1 <= rows, cols <= 200
  • kernel is square with odd size: 1 <= k <= 7
  • 0 <= image[r][c] <= 255
  • -10 <= kernel[i][j] <= 10

Function Signature

def apply_filter(image, kernel):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output