-1

I have a dark image which histogram lies in a tight group.

A source image

The image histogram

In image editor I can convert image int HSL colorspace, separate L channels and make histogram adjustment.

Histogram adjustment dialog

Is this the right way? Which next steps are necessary to do?

Is there a universal solution how to work with such histogram and improve image brightness/contrast?

How can I do the same thing with ImageMagick?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ivan Z
  • 1,517
  • 1
  • 16
  • 25
  • does that thing have a way to stretch the histogram to 1st and 99th percentile, per channel, while respecting gamma compression? -- that "tight group" is ***NOT*** the whole image content, it's just the bright background. your image content lies between that and blackness. – Christoph Rackwitz Mar 26 '23 at 19:19

2 Answers2

3

One simple method in Imagemagick is simply -auto-level.

Input:

enter image description here

convert img.png -auto-level result.png

enter image description here

If you want to replicate what you did, then in Unix syntax, convert your level parameters to percents: low=100*87/255=34.1% and high=100*141/255=55.3% and gamma=1.83

convert img.png -colorspace LAB -separate -write mpr:LAB \
\( mpr:LAB[0] -level 34.1%,55.3%,1.83 \) \
-swap 0,3 +delete \
-set colorspace LAB -combine -colorspace sRGB \
result.png

enter image description here

For Windows, remove the \ from the parentheses and change the end of line \ to ^. Double the % if in .bat file.

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • What does `-swap 0,3 +delete` do: swap two channels L and B? The other I think I understand – Ivan Z Mar 26 '23 at 19:46
  • It swaps the original L with the modified L then deletes the original L before Lmodified, A, B are recombined. The original L is image 0, A is image 1, B is image 2 and the new modified L is image 3 before the swap. – fmw42 Mar 26 '23 at 23:53
3

It's not clear to me what it is in your image that is of interest to you, so it's hard to know what to optimise for, so I just share my thoughts on processing it. Adapt, adopt and discard according to your needs.


Firstly, it has an entirely superfluous alpha channel, so I would discard that by starting any processing with:

magick image.png -alpha off ...

Secondly, the histograms for the three colours look almost identical in width and shape so I don't perceive there to be much colour information so I would go for greyscale processing. If we split the image into its RGB channels and lay them out side-by-side across the page with their contrast maximised we get:

magic image.png -alpha off -separate -auto-level +append RGB.jpg

enter image description here

There doesn't appear to me to be much difference between the channels, and in fact, the blue and the red look slightly noisier, so I would drop them and take green as my single channel. Take L of Lab mode in preference or make a greyscale conversion as you wish.


Now we need to look at contrast. As you have heavy vignetting (or similar) in the corners, and I presume that is not your area of interest, I would take the central 70% of the image and get its minimum and maximum value for use in a contrast stretch for the entire image:

magick pGmg9.png  -alpha off -gravity center -crop 70% -channel G -separate -format "%[min],%[max]" info:
13364,34695

So now I know the central region is between 16-bit brightness values of 13,364 to 34,695.


Now I want to use those two values as the lower and upper limits for a contrast-stretch, to which I can apply a gamma as well - but again I don't know what you are looking for:

Here's a gamma of 2:

convert pGmg9.png  -alpha off -channel G -separate -level 13364,34695,2 result.jpg   

enter image description here

Here's a gamma of 1:

convert pGmg9.png  -alpha off -channel G -separate -level 13364,34695,1 result.jpg  

enter image description here

Here's a gamma of 0.5:

convert pGmg9.png  -alpha off -channel G -separate -level 13364,34695,0.5 result.jpg

enter image description here


Or, you may prefer to simply discard the bottom 6% of darkest pixels and the top 3% of brightest pixels and do a linear stretch in between:

magick pGmg9.png  -alpha off -channel G -separate -contrast-stretch 6%x3% result.jpg

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432