I have an array of BitmapFrames and need to do a histogram stretch. I know this is different from a histogram equalization, and what the final outcome is... sorta. the problem i
If you have control over the lighting and or gain/offset of the camera, you can optimize those and stretch the histogram as you desire.
Don't forget to consider float. So a slight modification to dabonz413's answer:
maxVal = image.maximumValue() # 153
minVal = image.minumumValue() # 84
dynamic = maxVal-minVal
for pixel in image.Pixels():
newPixel = ((float) (pixel-minVal)/dynamic)*255
Histogram stretching is the mapping of pixel values such that:
In other words histogram stretching means stretching the dynamic of the image data (84:153) to the greatest possible dynamic (0:255).
This should not affect the hight of the histogram peaks, but only their spread (the illustration is a bit misleading on this point).
histogram stretch http://cct.rncan.gc.ca/resource/tutor/fundam/images/linstre.gif
Image source
In practice this is the mapping you would apply to the pixels of the image (pseudocode):
maxVal = image.maximumValue() # 153
minVal = image.minumumValue() # 84
dynamic = maxVal-minVal
for pixel in image.Pixels():
newPixel = ((pixel-minVal)/dynamic)*255