# How to do convolution of an image with many combinations of a mask, fast?

12 views (last 30 days)
Happy PhD on 11 Aug 2021
Answered: Image Analyst on 11 Aug 2021
Hi, I have an normal size image (about 1200x1900) for which I have cropped to a smaller image, but for simplicity lets say i have to analyse the whole image which is the worst case.
I have made an mask of ellipse, which in my case is an aperture, and I want to determine how much pixel intensity I have within the aperture. For every new mask (aperture) I vary the size in both x and y axis. So I get a line up of different ellipse mask sizes. In addition I want to rotate the mask 180 degrees to find all possible combinations it may be poistioned relative to my image. Thus, I get X number of masks that I want to do convolution with my original image. Is there a way to do this process as fast as I can?
At the moment I have two nested for-loops, for which I vary the ellipse size in the x and y axis, and for each combination I have another for-loop that rotate the mask, and then do convolution (conv2) of image and mask. I grab the result (mask size, intensity, and rotation angle) that gives me highest pixel intensity and move on to next mask size.
Preferably I want to do this without the for-loops because as you can image that this takes quite some time (for every large ellipse sizes (masks)). Up to an hour for really large apertures.
Any ideas?
Thanks!

Image Analyst on 11 Aug 2021
I think that's just hte way it is. You're not limited by the for loops, that's for sure. You can do literally millions of for loop iterations in a fraction of a second - just try it and see. Have a for loop with something very simple in it, like assigning a variable and you'll prove it to yourself. The real problem is the large convolutions. Not only are they large, or can be, but there are lots of them. I think you're stuck since the convolution functions conv2() and imfilter() are already highly optimized.
For what it's worth, I do a similar thing in the attached demo where I rotate a kernel to enhance fibers.

### Categories

Find more on Image Arithmetic in Help Center and File Exchange

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by