Image Translation

Computer Vision

Difficulty: 6 | Problem written by Sakar Ghimire
Problem reported in interviews at


We can use the following transformation matrix: 

\(\begin{pmatrix} 1 & 0 & t_x\\ 0 & 1 & t_y \end{pmatrix} \)

To translate an image, we then need to calculate the dot product of every pixel and this matrix. 

For a given grayscale input image represented by a NumPy array with shape (h,w), shift distance tuple (tx,ty), and the shape of output image as a tuple (h,w), calculate and return the translated output image. 

Sample Input:
<class 'numpy.ndarray'>
src_img: [[ 1 2 3 4 5] [ 1 1 1 1 1] [ 5 66 6 5 5]]
<class 'tuple'>
shift_distance: (0, 0)
<class 'tuple'>
shape_of_out_img: (5, 5)

Expected Output:
<class 'numpy.ndarray'>
[[ 0 0 0 0 0] [ 0 1 1 1 1] [ 0 66 6 5 5] [ 0 0 0 0 0] [ 0 0 0 0 0]]

This is a premium problem, to view more details of this problem please sign up for MLPro Premium. MLPro premium offers access to actual machine learning and data science interview questions and coding challenges commonly asked at tech companies all over the world

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Get access to Premium only exclusive educational content available to only Premium users.

Have an issue, the MLPro support team is available 24X7 to Premium users.

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

Log in to post a comment

Jump to comment-168
mo_venouziou • 2¬†months, 1¬†week ago


There appears to be an error in the test case. Shift distance is (0,0) but the solution has its first row and first column turned to zeros.


Input Test Case

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