0

Image Padding

Unsolved
Computer Vision

Difficulty: 3 | Problem written by mesakarghm
Problem reported in interviews at

Apple
Facebook
Google
Netflix

Image padding is an important step in digital image processing. It creates some additional pixels around the edges of an image, which allows us to maintain the image shape during various image processing operations, including convolutions in a CNN. 

Write a function pad(img, pad_val) which takes in a grayscale image (represented by a NumPy 2D array) and a padding value (int). 

The function will increase the shape of the input image by two pixels around both axes, and the new pixels will be set to set to the input padding value pad_val. 

If the shape of the input image is (h,w), then the shape of the output image will be (h+2, w+2).

 

Sample Input:
<class 'numpy.ndarray'>
img: [[ 1 7 9 13 12] [11 21 61 81 91] [ 5 66 6 5 5]]
<class 'int'>
pad_val: 15

Expected Output:
<class 'numpy.ndarray'>
[[15. 15. 15. 15. 15. 15. 15.] [15. 1. 7. 9. 13. 12. 15.] [15. 11. 21. 61. 81. 91. 15.] [15. 5. 66. 6. 5. 5. 15.] [15. 15. 15. 15. 15. 15. 15.]]

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Minus fugiat dolores distinctio quod illum suscipit architecto culpa quam, itaque asperiores provident soluta cumque blanditiis quidem et.

At sint molestiae quo odit aut fugit illum obcaecati quisquam, hic quos tempore omnis in voluptatum repellat voluptate ratione reprehenderit ab? Cum aspernatur alias ab incidunt, inventore voluptatum quaerat sunt aspernatur quam in eveniet fugiat quas, quis consequatur temporibus at culpa cupiditate.

Sequi tempore debitis repellat accusamus cum atque quibusdam dolor ipsam, recusandae eaque necessitatibus minus, praesentium ex corporis quasi quaerat sequi quo consequatur quisquam dolorum.

This is a premium feature.
To access this and other such features, click on upgrade below.

Ready.

Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)