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

Sobel Edge Detection

Computer Vision
Medium

The Sobel operator detects image edges by estimating horizontal and vertical intensity changes. Given a two-dimensional grayscale image, compute the gradient magnitude at every pixel.

Pad the image with one row or column of zeros on every side. Use the following horizontal kernel:

Kx=[−101−202−101]K_x = \begin{bmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix}Kx​=​−1−2−1​000​121​​

Use the following vertical kernel:

Ky=[−1−2−1000121]K_y = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{bmatrix}Ky​=​−101​−202​−101​​

For each image position with coordinates (i, j), center both kernels on that position and compute:

Gx(i,j)=∑a=02∑b=02Kx(a,b)P(i+a,j+b)G_x(i,j) = \sum_{a=0}^{2}\sum_{b=0}^{2} K_x(a,b)P(i+a,j+b)Gx​(i,j)=a=0∑2​b=0∑2​Kx​(a,b)P(i+a,j+b) Gy(i,j)=∑a=02∑b=02Ky(a,b)P(i+a,j+b)G_y(i,j) = \sum_{a=0}^{2}\sum_{b=0}^{2} K_y(a,b)P(i+a,j+b)Gy​(i,j)=a=0∑2​b=0∑2​Ky​(a,b)P(i+a,j+b)

Combine the two directional responses:

G(i,j)=Gx(i,j)2+Gy(i,j)2G(i,j) = \sqrt{G_x(i,j)^2 + G_y(i,j)^2}G(i,j)=Gx​(i,j)2+Gy​(i,j)2​

Here, P is the zero-padded image, i and j identify an output pixel, and a and b identify a kernel position.

Return G as a two-dimensional list of floats with the same height and width as the input image.

Loading visualization...

Examples

Input: image = [[0, 0, 10, 10], [0, 0, 10, 10], [0, 0, 10, 10]]

Output: [[0.0, 31.622776601683793, 42.42640687119285, 42.42640687119285], [0.0, 40.0, 40.0, 40.0], [0.0, 31.622776601683793, 42.42640687119285, 42.42640687119285]]

Explanation: The vertical intensity transition produces large horizontal Sobel responses.

Input: image = [[100]]

Output: [[0.0]]

Hint 1

Copy the image into the center of a grid with a one-pixel zero border.

Hint 2

Accumulate horizontal and vertical kernel responses before taking their Euclidean magnitude.

Requirements

  • Apply zero-padding before convolution
  • Compute both Gx and Gy using the standard Sobel kernels
  • Return the magnitude as sqrt(Gx^2 + Gy^2) for each pixel
  • Output has the same dimensions as the input

Constraints

  • Image has at least one pixel
  • Pixel values are non-negative numbers
  • Return a 2D list of floats with the same shape as the input
  • Time limit: 300 ms
Try Similar Problems
Gaussian Blur KernelConv2d Image FilteringMorphological OperationsImage HistogramHistogram Equalization

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Sobel Edge Detection

Computer Vision
Medium

The Sobel operator detects image edges by estimating horizontal and vertical intensity changes. Given a two-dimensional grayscale image, compute the gradient magnitude at every pixel.

Pad the image with one row or column of zeros on every side. Use the following horizontal kernel:

Kx=[−101−202−101]K_x = \begin{bmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix}Kx​=​−1−2−1​000​121​​

Use the following vertical kernel:

Ky=[−1−2−1000121]K_y = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{bmatrix}Ky​=​−101​−202​−101​​

For each image position with coordinates (i, j), center both kernels on that position and compute:

Gx(i,j)=∑a=02∑b=02Kx(a,b)P(i+a,j+b)G_x(i,j) = \sum_{a=0}^{2}\sum_{b=0}^{2} K_x(a,b)P(i+a,j+b)Gx​(i,j)=a=0∑2​b=0∑2​Kx​(a,b)P(i+a,j+b) Gy(i,j)=∑a=02∑b=02Ky(a,b)P(i+a,j+b)G_y(i,j) = \sum_{a=0}^{2}\sum_{b=0}^{2} K_y(a,b)P(i+a,j+b)Gy​(i,j)=a=0∑2​b=0∑2​Ky​(a,b)P(i+a,j+b)

Combine the two directional responses:

G(i,j)=Gx(i,j)2+Gy(i,j)2G(i,j) = \sqrt{G_x(i,j)^2 + G_y(i,j)^2}G(i,j)=Gx​(i,j)2+Gy​(i,j)2​

Here, P is the zero-padded image, i and j identify an output pixel, and a and b identify a kernel position.

Return G as a two-dimensional list of floats with the same height and width as the input image.

Loading visualization...

Examples

Input: image = [[0, 0, 10, 10], [0, 0, 10, 10], [0, 0, 10, 10]]

Output: [[0.0, 31.622776601683793, 42.42640687119285, 42.42640687119285], [0.0, 40.0, 40.0, 40.0], [0.0, 31.622776601683793, 42.42640687119285, 42.42640687119285]]

Explanation: The vertical intensity transition produces large horizontal Sobel responses.

Input: image = [[100]]

Output: [[0.0]]

Hint 1

Copy the image into the center of a grid with a one-pixel zero border.

Hint 2

Accumulate horizontal and vertical kernel responses before taking their Euclidean magnitude.

Requirements

  • Apply zero-padding before convolution
  • Compute both Gx and Gy using the standard Sobel kernels
  • Return the magnitude as sqrt(Gx^2 + Gy^2) for each pixel
  • Output has the same dimensions as the input

Constraints

  • Image has at least one pixel
  • Pixel values are non-negative numbers
  • Return a 2D list of floats with the same shape as the input
  • Time limit: 300 ms
Try Similar Problems
Gaussian Blur KernelConv2d Image FilteringMorphological OperationsImage HistogramHistogram Equalization

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.