1 | initial version |
You could implement some kind of tree search. Start with a point in your mask and add its neighbours to a stack if they are also in the masked region and remove (set to zero) your starting point. Do the same until your stack is empty. This gives you a running time of O(N) if N is the number of pixels in your mask instead of O(colsrows). This of course only works well if your mask is basically one region. If it consists of many small regions, you will have to look at your whole picture to find each region so that you could again end up in something like O(colsrows).