Joe Healey
Joe Healey

Reputation: 1252

Setting specific image brightness

I'll get this out of the way at the start: this might be a really stupid question, and it may also belong on a different SE, so please feel free to tell me if that's the case.

I have a bunch of images, which are merges of a greyscale and green channel. Some of the images are much darker than others, while the rest are all approximately the same.

I'd like to 'homogenise' the brightness of the set of images as best as I can (it doesn't have to be perfect).

Does anyone know if there is a reasonably simple way to do this?

This is how I'm thinking to do it at present, utilising ImageMagick (a bit of bash pseudo- and actual code, since I'm using CLI imagemagick on OSX, but other solutions would be fine):

Step 1

Read in my set of 'reference images' with a brightness/grey level I'm OK with and get an average grey level:

greyvals = ()
for file in an_array_of_image_files ; do
     # get array of grey values
     greyval=$(convert $file -colorspace Gray -format "%[mean]" info:)
     greyvals+=$greyval

# average the greyvals of the reference set through some mean function.

Step 2

This is where my question really lies. Is there a way to brighten or darken an image to a specified grey level?

ImageMagick provides the modulate function, but the examples I've found so far require a percentage 'brightening/darkening', e.g.:

convert $file -modulate 200% ${file%.*}_bright.png

Am I barking up the wrong tree completely?


EDIT

Some example images:

A reference 'bright enough' image:

enter image description here

An example 'dark' image:

enter image description here

Histogram equalisation image - this appears to work quite nicely, but is introducing some white artifacts in certain regions. enter image description here

Upvotes: 2

Views: 2057

Answers (2)

fmw42
fmw42

Reputation: 53164

Using my ImageMagick scripts, histmatch and matchimage, here are a few results using your two images. ImageMagick 6.9.9.43 Q16 and Mac OSX Sierra.

histmatch -c rgb reference.jpg dark.png dark_histmatch_rgb.png

enter image description here

histmatch -c gray reference.jpg dark.png dark_histmatch_gray.png

enter image description here

matchimage -c hsi dark.png reference.jpg dark_matchimage_hsi.png

enter image description here

matchimage -c ycbcr dark.png reference.jpg dark_matchimage_ycbcr.png

enter image description here

Upvotes: 1

GeeMack
GeeMack

Reputation: 5395

Using ImageMagick 7 I would consider this approach...

magick input.png -brightness-contrast %[fx:50-(mean*100)] output.png

That would adjust every input image to have a 50% mean brightness overall. Using IM6 you can get the required value into a variable with a command like this...

adjuster=`convert input.png -format %[fx:50-(mean*100)] info:`

Then use that variable as an argument for the "-brightness-contrast" operator in a command like my IM7 example above, something like this...

convert input.png -brightness-contrast $adjuster output.png

I haven't tested this from a *nix command line, but the concept should work.

Upvotes: 2

Related Questions