At first I thought I could do computer vision for Detection of foreground pixels from background pixels but that would have been too much for processing since I was struggling for higher dimensions. Even with the code I have below would work slow, I tested on 10000x10000 but couldn't on 100000x100000 since that was huge for my simple processing power. Then I thought of applying what I learnt in a Rust course about parallelism and chunking, first I tried multiprocessing library but it didn't work well so switched to Chunking which worked better
import numpy as np