0

My algorithm consists in convoluting a fixed size kernel around an image and at each step look at the borders of the portion of the image. If all the borders are equal to zero, set all the portion of the image to zero, else, do nothing.

Code:

def iterForms(img):
    height, width = img.shape
    size = 14

    for i in range(0, height - size - 2):
        for j in range(0, width - size - 2):
            crop = img[i : i + size + 2, j : j + size + 2]
            cleanCrop(crop)
    return img


def cleanCrop(crop):
    h, w = crop.shape
    borders = np.vstack((crop[0,:], crop[h-1,:], crop[:,0], crop[:,w-1]))
    if np.all(borders == 0):
        crop[:, :] = 0

I was wondering if there is a optimized function that can convolute around an image and for each step execute a function (in this case the function is 'cleanCrop'). If it does not exist, can I use multi-threding to optimize this process? Thanks in advance.

EDIT : It's not technically Convolution. Let's just call it Sliding around the image.

G-Man
  • 11
  • 3
  • I understand what you mean, but I think this is technically not convolution. Convolution is not just sliding a window around the domain of a function (an image in this case) but [also implies](https://en.wikipedia.org/wiki/Convolution#Definition) that you are integrating over the product. Which is sad for you, because the common way to optimize convolutions is to use an FFT, which won't work here. – Thomas Jan 30 '19 at 12:09
  • You could try running this in pypy and see if that gives you the speedup you need. – Thomas Jan 30 '19 at 12:10
  • Or if your goal is just to get rid of small blobs in the image, regardless of whether they fit exactly inside this 14x14 square, you could consider a morphological opening followed by closing. Or a connected components detection. – Thomas Jan 30 '19 at 12:13
  • Yes, it's not technically convolution, my bad. I will try your suggestions and give the results. Thanks – G-Man Jan 30 '19 at 12:27
  • Technically, you can *"slide around"* like this... https://stackoverflow.com/a/54440152/2836621 – Mark Setchell Jan 30 '19 at 12:54

0 Answers0