Search code examples
bashimage-processingimagemagickimage-manipulation

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


Solution

  • 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