The issue with converting hue to grayscale is that red becomes black, but so does any gray in the image. That is that gray (from black to white) is indeterminant in the usual HSL and HSB transformation when looking just a hue. Usually gray is then coded as black same as red. So one needs to convert gray to some other shade before doing the auto-level. In the first example, I found that magenta worked for reasonable color before converting to HSB, so that after auto-level it would stretch appropriately to match the given colors. So I am not surprised that it is image specific. Gray masking is needed to preserve the white background and the black lettering.
Here is a modified algorithm. It uses the H,S and B channels to do the masking. But the S and B channels need to be properly thresholded to avoid white artifacts and get the graph ticks and step wedge to show and that is still image dependent. You need to look at the HSB channels to see how to threshold them.
This is the basic algorithm
infile="3D_conrod_iso.png"
hueshift=XX
# shift the hue towards red, if red is bluish-red and not yellowish-red; otherwise red turns to white and not black
satthresh=XX
# S threshold
brithresh=XX
# B threshold
inname=`convert $infile -format "%t" info:`
hueval=$((100+hueshift))
# first line -- modulate to shift hue if needed and convert and separate HSB channels
# second line -- threshold the S channel and negate for masking to preserve the white background
# third line -- composite the H channel with the thresholded and negated S channel
# fourth line --threshold the B channel
# fifth line -- delete the tmp files and composite the previous composite with the thresholded B channel
convert $infile -modulate 100,100,$hueval -colorspace HSB -separate +channel \
\( -clone 1 -threshold $satthresh% -negate \) \
\( -clone 0 -clone 3 -clone 3 -compose over -composite \) \
\( -clone 2 -threshold $brithresh% \) \
-delete 0-3 -compose multiply -composite \
${inname}_gray.png
Here are the 3 test image results:
infile="test.png"
hueshift=0
satthresh=80
brithresh=93
inname=`convert $infile -format "%t" info:`
hueval=$((100+hueshift))
convert $infile -modulate 100,100,$hueval -colorspace HSB -separate +channel \
\( -clone 1 -threshold $satthresh% -negate \) \
\( -clone 0 -clone 3 -clone 3 -compose over -composite \) \
\( -clone 2 -threshold $brithresh% \) \
-delete 0-3 -compose multiply -composite \
${inname}_gray.png
infile="cone_ls2.png"
hueshift=0
satthresh=15
brithresh=85
inname=`convert $infile -format "%t" info:`
hueval=$((100+hueshift))
convert $infile -modulate 100,100,$hueval -colorspace HSB -separate +channel \
\( -clone 1 -threshold $satthresh% -negate \) \
\( -clone 0 -clone 3 -clone 3 -compose over -composite \) \
\( -clone 2 -threshold $brithresh% \) \
-delete 0-3 -compose multiply -composite \
${inname}_gray.png
infile="3D_conrod_iso.png"
hueshift=3
satthresh=10
brithresh=50
inname=`convert $infile -format "%t" info:`
hueval=$((100+hueshift))
convert $infile -modulate 100,100,$hueval -colorspace HSB -separate +channel \
\( -clone 1 -threshold $satthresh% -negate \) \
\( -clone 0 -clone 3 -clone 3 -compose over -composite \) \
\( -clone 2 -threshold $brithresh% \) \
-delete 0-3 -compose multiply -composite \
${inname}_gray.png