Processing indexed colour (grey)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ozbigben
Posts: 27
Joined: 2012-03-25T02:15:27-07:00
Authentication code: 8675308

Processing indexed colour (grey)

Post by ozbigben »

Hi All

I'm trying to do some grading of scans of microfilm (350,000+). The scanner has output indexed colour (greyscale) scans. My problem is that some of the images are very dark(1%) and about 20% could use some degree of lightening. I've worked through grouping the images by appearance using ImageJ and then determined a levels adjustment for each group using Photoshop. Having sorted out the settings I'm now looking to automate the processing with ImageMagick. The adjustments I'm making are substantial increases in gamma with some slight tweaks to black levels to deal with the very dark scans.

Problem 1: No matter what method of greyscale conversion I use I can't get a 1:1 conversion for the pixel values (RGB are all the same) The differences are minor but as I'm working on a very small range of low pixel values, having a few indexed colours drop to 0 is significant.

Code: Select all

 -grayscale Average

Code: Select all

 -set colorspace Gray -separate -evaluate-sequence Mean
Problem 2: My levels adjustments look very different to what I get with Photoshop. I've tried 0-255 and % for black/white levels and even a separate gamma adjustment before/after levels. (gamma adjustments in the order of 2.8 - 4)

I'm working with the latest IM version on Win7 x64. Any advice/suggestions would be welcomed

Ben
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Processing indexed colour (grey)

Post by fmw42 »

I am having some trouble understanding what you are doing and what you want. Please upload an example image to some free hosting service and put the URL here. If you have an example result, post that also and tell us what is wrong.

If your image is already grayscale, then why do you need to convert to grayscale again?

Are you trying to get an overall average grayscale value for the whole image?

For automatic adjustment try a combination of -auto-gamma -auto-level or the reverse order.
ozbigben
Posts: 27
Joined: 2012-03-25T02:15:27-07:00
Authentication code: 8675308

Re: Processing indexed colour (grey)

Post by ozbigben »

Locked myself out by changing my email.... back now :) Yes, I should have been brief.
Original image
http://files.digitisation.unimelb.edu.a ... EEL_01.tif
Image

Photoshop, Convert to greyscale, Levels: black 20, white 150, gamma 3
Image

IM: magick %1 -set colorspace Gray -separate -evaluate-sequence Mean -level 20,150,3 -quality 70 %~dpn1_IM_lvl20_150_3.jpg
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Processing indexed colour (grey)

Post by fmw42 »

try this using -contrast-stretch and -level. IM needs values in quantum range or percent. So I always convert to percent, since I do not know what your quantum range is for your compile of IM. PS always provides value in the range 0 to 255. As I recall PS levels uses default clip values of 0.01%, but I might remember wrong and it could be 0.015%. That is why I have -contrast stretch there.

Unix syntax. (Sorry I do not know how to specify variables in Windows syntax, but you can substitute into the last convert with just the values bp=7.84314; wp=58.8235;)

Code: Select all

bp=20
wp=150
gamma=3
bp=`convert xc: -format "%[fx:100*$bp/255]" info:`
wp=`convert xc: -format "%[fx:100*$wp/255]" info:`
convert 007106_REEL_01.tif -contrast-stretch 0.01%,0.01% -level $bp%,$wp%,$gamma result.jpg

Thus in any OS, use

Code: Select all

convert 007106_REEL_01.tif -contrast-stretch 0.01%,0.01% -level 7.84%,58.82%,3 result.jpg

See
http://www.imagemagick.org/script/comma ... st-stretch
http://www.imagemagick.org/script/comma ... .php#level
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Processing indexed colour (grey)

Post by snibgo »

Your image is already grayscale, so "-set colorspace Gray -separate -evaluate-sequence Mean" does nothing.

Your input has only 16 shades of gray, so your output will also have only 16 shades.

But this works okay:

Code: Select all

convert 007106_REEL_01.tiff -auto-gamma -auto-level out.png
snibgo's IM pages: im.snibgo.com
ozbigben
Posts: 27
Joined: 2012-03-25T02:15:27-07:00
Authentication code: 8675308

Re: Processing indexed colour (grey)

Post by ozbigben »

The TIFFs are indexed colour (correction: oddly a mix of indexed and greyscale)... visually the same but I'd prefer greyscale. I hadn't tried the auto options as it wasn't that successful in Photoshop but the one image I tested looked promising with IM. I'll do some further testing across the range of contrasts to see what it does. I might run a second tweak but that won't be as severe an adjustment. Thanks :)
ozbigben
Posts: 27
Joined: 2012-03-25T02:15:27-07:00
Authentication code: 8675308

Re: Processing indexed colour (grey)

Post by ozbigben »

The second tweak worked nicely and I'm curious if it's possible to do it all in ImageMagick.
Image

Code: Select all

magick  in.file -auto-level -auto-gamma -set colorspace Gray -separate -evaluate-sequence Mean -gamma NNN out.file
The value for the last gamma adjustment is conditional on the median density of the original image. 2.2 if median < 128 or 1.2 if median >=128
ozbigben
Posts: 27
Joined: 2012-03-25T02:15:27-07:00
Authentication code: 8675308

Re: Processing indexed colour (grey)

Post by ozbigben »

fmw42 wrote: 2017-08-03T18:08:50-07:00 try this using -contrast-stretch and -level. IM needs values in quantum range or percent. So I always convert to percent, since I do not know what your quantum range is for your compile of IM. PS always provides value in the range 0 to 255. As I recall PS levels uses default clip values of 0.01%, but I might remember wrong and it could be 0.015%. That is why I have -contrast stretch there.
Thanks Fred... I'm a bit rusty on IM usage and wasn't accounting for IM's quantum range. Always using % is a good idea. I reset Photoshop's clipping to 0 so the linear stretch is the same but I wasn't getting consistent quality on auto contrast adjustments. I prefer IM in these situations as it's much faster loading/saving images which is a big factor with large numbers of images.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Processing indexed colour (grey)

Post by snibgo »

ozbigben wrote:The value for the last gamma adjustment is conditional on the median density of the original image. 2.2 if median < 128 or 1.2 if median >=128
IM doesn't have a built-in calculator for median. It can be done by finding the 50% level of a cumulative histogram.

Fred has a bash script for this: http://www.fmwconcepts.com/imagemagick/median/index.php

Code: Select all

./median 007106_REEL_01.tiff
gray: 68
I have a process module for this:

Code: Select all

convert 007106_REEL_01.tiff -colorspace Gray -process 'mkhisto cumul norm' -process 'mkhisto cumul norm' -gravity Center -crop 1x1+0+0 +repage txt:

# ImageMagick pixel enumeration: 1,1,4294967295,gray
0,0: (1.14085e+09,1.14085e+09,1.14085e+09)  #444444  gray(68)
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Processing indexed colour (grey)

Post by fmw42 »

I reset Photoshop's clipping to 0 so the linear stretch is the same but I wasn't getting consistent quality on auto contrast adjustments.
If you set PS clipping to 0, then remove my -contrast-stretch completely.

Code: Select all

convert 007106_REEL_01.tif -level 7.84%,58.82%,3 result.jpg
ozbigben
Posts: 27
Joined: 2012-03-25T02:15:27-07:00
Authentication code: 8675308

Re: Processing indexed colour (grey)

Post by ozbigben »

Thanks for the suggestions. I've done a test run on all of the images and it's worked pretty well. There are still a few other exceptions with low contrast images. I have to figure out a way to classify these first but it's looking like a combination of median, mean and stdev. I've got an Ubuntu machine now so that should give us some extra options.
Post Reply