Improve image contrast using specific threshold values

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
cuthbert

Improve image contrast using specific threshold values

Post 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!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Improve image contrast using specific threshold values

Post 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
cuthbert

Re: Improve image contrast using specific threshold values

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Improve image contrast using specific threshold values

Post 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
cuthbert

Re: Improve image contrast using specific threshold values

Post by cuthbert »

Thanks again, fmw42.

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