Added some dilation and erosion to the threshold mask to improve the results.
Added code to generate some example images of the threshold function + created some of those images.