Fill regions from contour based on cumsum
I got an image for scene classification:

And using cumsum, I want to segment the three parts of it.
I take this simple operation in pytorch (can use tensorflow of course too, or python)

You can use torch.flipud to perform the cumsum in both directions:
mask = (src_img.cumsum(dim=0) &gt;0 ) + 2* torch.flipud(torch.flipud(src_img).cumsum(dim=0)&gt;0)

Resulting with:

