Mosaicing in Python or C++?

Asked

Viewed 74 times

-1

I am developing a program that, in the future, I intend to transform into app for android. I’m developing in Python, my idea was to use kivy to develop the entire app.

In the current step I am having problems generating a mosaic of images. The code works (I developed based on this tutorial: https://towardsdatascience.com/image-stitching-using-opencv-817779c86a83), the problem is that it is very slow, perhaps by the size of the images (4032 x 3024). A set of 4 images took about 20 minutes to mosaic on my notebook (i7 6a gen. and 8 GB of RAM), this will become unviable to run on android.

Having said that, I’m looking for solutions to make it faster. Maybe rewrite the code to a faster language like C++, what do you think? Any suggestions to optimize code execution speed?

1 answer

4


Doing it in C++ won’t make it much faster. This is because although Python is rather a slow language, all the calls that actually consume resources in this algorithm (and other similar ones), are executed in native code.

That is, when you call something like matches = bf.knnMatch(des1,des2, k=2) (which is possibly one of the most time-consuming calls), all that is executed is already in native code, written in an optimized language. Not only that, but it’s in a library that’s optimized over several years by tens or hundreds of volunteers - possibly making use of resources like SIMD or the Gpus themselves. If you rewrite the entire algorithm in C++, you will certainly get a performance reversed to Opencv. If you make a code in C++ using Opencv as well, you will be using exactly the same code that is used from Python.

You didn’t post all your code, and you didn’t make one Profiling- then it is not possible to help with details. But yes, probably the problem is because of the size of the images (many academic algorithms are developed with almost "symbolic" images in size compared to the size of actual photos). In that case, all you need to do is diminish the images before calling the costly algorithms - in the excerpt:

img_ = cv2.imread(‘right.JPG’)
img1 = cv2.cvtColor(img_,cv2.COLOR_BGR2GRAY)
img = cv2.imread(‘left.JPG’)
img2 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

that separates black and white copies img1 and img2 where the algorithms will be executed, take advantage and resize - to these image sizes maybe 10% of the image will be enough for this algorithm (that is, to go from 4000 to 400px wide) - and in this case, the whole program can run up to 100X faster.

And then, after obtaining the snap coordinates by the Opencv algorithms, just multiply the values back by the scale factor before plotting the final (colored) images with matplotlib.

  • Thank you so much for your help! As this is a code that I develop in my work, I was not allowed to post in full, the mosaicing is just one of the steps. Again, thank you so much!

  • I made some modifications that made the code faster. One of them was the language, rewritten in C++ using also Opencv, there was a big jump in runtime with the images in the original size, in Python it was taking about 20 minutes, in C++ it fell to about 7 minutes. Using your tip, jsbueno, I reduced the running time even more, I managed to reach about 37 seconds, which for my application is great. ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.