How to speed up time for your own translation image function #2

rocky king
4 min readAug 27, 2020

--

From last previous post I explain about how to move image to some location using matrix. You can read it from https://medium.com/@bosssds65/translation-image-using-translation-matrix-with-python-d5d2b580d963

In this chapter, I will explain how to speed up time for translation image function that I created from previous post. First of all, I check performance of translation function in OpenCV library by use distance to shifted image is x = 100, y = 100 and show in this code below.

OpenCV use duration of translation image is 0.000997 s then keep in mind.Then I check my own translation image function that I created in previous pose by use distance to shifted image is x=100,y=100 equal above following code below .

Then I check my own translation image function that I created in previous pose by use distance to shifted image is x=100,y=100 equal above following code below .

When you compare this performance, you will see a big different of time. Then I have a question in my mind, why translation function in OpenCV is so fast. So, I think how to speed up my function then I found the solution.

I approach this problem from my previous post I calculation in every point of image but it isn’t always to using it. Because we can calculate with first one point of image and use slide data in numpy to help it. I separate to 4 case.

Case 1 is distance to shifted image is x= +,y = + (both positive)

From above image new_img is some part of old image which can use sliding in numpy and have constrain is not over w,h and greater than 0.

Case 2 is distance to shifted image is x= -,y = + (negative, positive)

In python programming index of data if we put negative index it mean position that count from last index . So, if x is negative I will use width = — new_x to sllde data

Case 3 is distance to shifted image is x= +,y = — (positive , negative)

For this case it same above but use negative index in y axis

Case 4 is distance to shifted image is x= -,y = — (both negative)

In this case, using both negative index this code below

Code below is full code of speed up translation image function

Last I check image output and performance again

Performance of speed up translation image function is very quick!! . it approximately with OpenCV library.

Then check image to test correction of output

You will see this code in this github

--

--