Instead of iterating over each row and column, you can move the array left, right, up and down for the appropriate number of elements. With each shift, you accumulate your values ββin the base array. After changing and accumulating, you calculate the average value and use your threshold to return the mask. See this post for a general discussion on this topic. The idea is to use numpy translation, which will apply a function or operator to all elements of the array in C, not Python.
I adapted the code from the linked message according to what I think you are trying to execute. In any case, the overall picture should speed up the process. You must decide what to do with the edges in the return mask. Here, I just set the return mask to False, but you can also eliminate the edges by expanding the input by one pixel in each direction and filling the nearest pixel, zeros, gray, etc.
def findRegions(self,data):
source share