3

I have roughly 160 images for an experiment. Some of the images, however, have clearly different levels of brightness and contrast compared to others. For instance, I have something like the two pictures below:

enter image description here enter image description here

I would like to equalize the two pictures in terms of brightness and contrast (probably find some level in the middle and not equate one image to another - though this could be okay if that makes things easier). Would anyone have any suggestions as to how to go about this? I'm not really familiar with image analysis in Matlab so please bear with my follow-up questions should they arise. There is a question for Equalizing luminance, brightness and contrast for a set of images already on here but the code doesn't make much sense to me (due to my lack of experience working with images in Matlab).

Currently, I use Gimp to manipulate images but it's time consuming with 160 images and also just going with subjective eye judgment isn't very reliable. Thank you!

Community
  • 1
  • 1
A.Rainer
  • 719
  • 5
  • 16
  • 1
    Possibly [`histeq` command](http://www.mathworks.com/help/images/ref/histeq.html) can be of help to you. – dfrib Feb 25 '16 at 16:24
  • I've tried that but the results vary from one picture to another. – A.Rainer Feb 25 '16 at 17:49
  • Double-checking: when you've tried it, have you used a pre-set histogram; pre-set counts for equally spaced bins with intensity values? _"... transforms the intensity image `I` so that the histogram of the output intensity image `J` with `length(hgram)` bins approximately matches `hgram`"_. – dfrib Feb 25 '16 at 18:04
  • dfri, I didn't use a pre-set histogram. Sorry, this seems to be such a basic question but would you mind telling me how to do that? – A.Rainer Feb 25 '16 at 18:18
  • Hi, did you find the solution using gimp? Or imagemagick? – Kostanos Mar 13 '19 at 07:15
  • 1
    No, I didn't. But one of my labmates recently worked on this. I'll check with her how she did it. – A.Rainer Mar 14 '19 at 12:41

1 Answers1

2

You can use histeq to perform histogram specification where the algorithm will try its best to make the target image match the distribution of intensities / histogram of a source image. This is also called histogram matching and you can read up about it on my previous answer.

In effect, the distribution of intensities between the two images should hopefully be the same. If you want to take advantage of this using histeq, you can specify an additional parameter that specifies the target histogram. Therefore, the input image would try and match itself to the target histogram. Something like this would work assuming you have the images stored in im1 and im2:

out = histeq(im1, imhist(im2));

However, imhistmatch is the more better version to use. It's almost the same way you'd call histeq except you don't have to manually compute the histogram. You just specify the actual image to match itself:

out = imhistmatch(im1, im2);

Here's a running example using your two images. Note that I'll opt to use imhistmatch instead. I read in the two images directly from StackOverflow, I perform a histogram matching so that the first image matches in intensity distribution with the second image and we show this result all in one window.

im1 = imread('https://i.stack.imgur.com/oaopV.png');
im2 = imread('https://i.stack.imgur.com/4fQPq.png');
out = imhistmatch(im1, im2);
figure;
subplot(1,3,1);
imshow(im1);
subplot(1,3,2);
imshow(im2);
subplot(1,3,3);
imshow(out);

This is what I get:

enter image description here

Note that the first image now more or less matches in distribution with the second image.

We can also flip it around and make the first image the source and we can try and match the second image to the first image. Just flip the two parameters with imhistmatch:

out = imhistmatch(im2, im1);

Repeating the above code to display the figure, I get this:

enter image description here

That looks a little more interesting. We can definitely see the shape of the second image's eyes, and some of the facial features are more pronounced.


As such, what you can finally do in the end is choose a good representative image that has the best brightness and contrast, then loop over each of the other images and call imhistmatch each time using this source image as the reference so that the other images will try and match their distribution of intensities to this source image. I can't really write code for this because I don't know how you are storing these images in MATLAB. If you share some of that code, I'd love to write more.

Community
  • 1
  • 1
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Rayryeng, thank you so much for your help! This certainly adress part of the issue but the image quality does seem to be affected. I also found a potential solution at: http://www.mathworks.com/matlabcentral/fileexchange/28972-custom-shaped-histogram If this works, I will post it here. – A.Rainer Feb 25 '16 at 20:09