dynamically determining threshold based on average pixel value