Page 1 of 1

Improve image contrast using specific threshold values

Posted: 2009-09-07T02:17:51-07:00
by cuthbert
I want to improve the contrast of certain type of images by getting rid of the pixel values below and above a certain threshold, then renormalizing the central pixel range.

I have tried using black-threshold and white-threshold, but these methods dont exactly do what I want, because they set all values below the threshold to 0 (black-threshold) and all values above the threshold to max (white-threshold). Thus my resulting image doesnt actually give any improved contrast.

In essence, this is what I want to do:
Set all pixel values that are below 50 equal to 50.
Set all pixel values that are above 200 equal to 200.

If there is some way I can achieve this in ImageMagick, then I can simply normalize the image afterwards and I will have an image with good contrast for the pixel range that I am interested in.

If someone know a way of doing this I would be very happy!

Re: Improve image contrast using specific threshold values

Posted: 2009-09-07T10:56:29-07:00
by fmw42
see -contrast-stretch X,Y% or -linear-stretch X,Y%

That will threshold between the two X,Y percent values equivalent graylevel making anything at X% or below to black, anything at Y% or above equal to white and stretch everything in between linearly.

see

http://www.imagemagick.org/script/comma ... st-stretch
http://www.imagemagick.org/script/comma ... ar-stretch
http://www.imagemagick.org/Usage/color/ ... st-stretch
http://www.imagemagick.org/Usage/color/#linear-stretch

Re: Improve image contrast using specific threshold values

Posted: 2009-09-07T12:55:49-07:00
by cuthbert
Thank you for your suggestion, fmw42.

However, linear-stretch/contrast-stretch doesn't quite work for me either, because I only know the values of the pixels I want to get rid of, I don't know the number of pixels! So unless there is a method in IM I can use to count the number of pixels in the image below/above a specific pixel value, I cant use linear-stretch, it seems.

Any other suggestions?

Re: Improve image contrast using specific threshold values

Posted: 2009-09-07T19:09:46-07:00
by fmw42
try -level

it stretches the images between the lower and higher graylevels and clips anything above or below.

see

http://www.imagemagick.org/Usage/color/#levels

Re: Improve image contrast using specific threshold values

Posted: 2009-09-09T13:04:25-07:00
by cuthbert
Thanks again, fmw42.

Problem solved.
Turns out -level was EXACTLY what I was looking for!