0

I am using the convolution code from here but instead of having the result of the convolution be k = (roi * K).sum() I want to be able to change the operation applied to the (roi * K). For instance: np.std(roi * K) or min(roi * K).

Unfortunatelly this code is not optimized to run fast, and I wanted it to go faster.

I tried to find an already implemented method like this but I didn't find any. If there is anything like this out there with fast execution that would be great. If not what would be the best strategy to optimize this code?

Tweaking the code a bit to print the execution time this is the result:

[INFO] applying small_blur kernel
Convolve Time: 2.21276
OpenCV Time: 0.00088
Ratio Convolve/OpenCV: 2519.95248
[INFO] applying large_blur kernel
Convolve Time: 2.50598
OpenCV Time: 0.00611
Ratio Convolve/OpenCV: 410.16292
[INFO] applying sharpen kernel
Convolve Time: 2.10106
OpenCV Time: 0.00027
Ratio Convolve/OpenCV: 7750.65084
[INFO] applying laplacian kernel
Convolve Time: 2.10883
OpenCV Time: 0.00019
Ratio Convolve/OpenCV: 11111.88317
[INFO] applying sobel_x kernel
Convolve Time: 2.16267
OpenCV Time: 0.00021
Ratio Convolve/OpenCV: 10474.46882
[INFO] applying sobel_y kernel
Convolve Time: 2.09571
OpenCV Time: 0.00022
Ratio Convolve/OpenCV: 9513.05519
[INFO] applying emboss kernel
Convolve Time: 2.10961
OpenCV Time: 0.00026
Ratio Convolve/OpenCV: 8125.21671
8
  • 1
    Why not just use OpenCV convolution --- filter2D? See docs.opencv.org/3.0-beta/modules/imgproc/doc/… or pyimagesearch.com/2016/07/25/…. In OpenCV, you can even use GPU to make it even faster. Commented Aug 4, 2019 at 0:26
  • @fmw42 I don't see how this filter2D would solve the problem. The kernel must be a "single-channel floating point matrix" not a function I pass to it. Commented Aug 4, 2019 at 14:54
  • Can you not convert your function into a set of kernel elements? Commented Aug 4, 2019 at 17:40
  • I am not sure if thats possible. What would you do to transform np.std() into a set of kernels? Commented Aug 4, 2019 at 23:33
  • Perhaps I do not understand. All the kernels K in the reference you link are 2D matrices. I do not understand how or why you want to use a function. What do you mean or expect from np.std(roi * K). Can you explain functionally what you are trying to do? Do you simply want the standard deviation of each pixel in the roi using a give neighborhood size? Commented Aug 4, 2019 at 23:39

1 Answer 1

2

You can try looking at numba

Numba translates Python functions to optimized machine code at runtime using the industry-standard LLVM compiler library. Numba-compiled numerical algorithms in Python can approach the speeds of C or FORTRAN.

If your code is numerically orientated (does a lot of math), uses NumPy a lot and/or has a lot of loops, then Numba is often a good choice. In these examples we’ll apply the most fundamental of Numba’s JIT decorators, @jit, to try and speed up some functions to demonstrate what works well and what does not.

You can install it by

$ pip install numba

and usage is as simple as calling the @jit decorator on top of your functions.

Sign up to request clarification or add additional context in comments.

3 Comments

So in the case I have a method using OpenCV I should try to change it to Numpy and then use Numba? Or is it going to speed it up by just adding the @jit decorator anyway?
Numba basically speeds calculations up. Although i am not 100% certain but you can try it on the opencv function first without converting it to numpy.
I tried with both OpenCV and Numpy but it returns an error (I don't remember exactly what the error was right now but it was something related to compatibility). The error was on the np.pad() and the cv2.copyMakeBorder() functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.