Reputation: 1252
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):
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.
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:
An example 'dark' image:
Histogram equalisation image - this appears to work quite nicely, but is introducing some white artifacts in certain regions.
Upvotes: 2
Views: 2057
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
histmatch -c gray reference.jpg dark.png dark_histmatch_gray.png
matchimage -c hsi dark.png reference.jpg dark_matchimage_hsi.png
matchimage -c ycbcr dark.png reference.jpg dark_matchimage_ycbcr.png
Upvotes: 1
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