halo minimization by local min/max smooth clamping

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: halo minimization by local min/max smooth clamping

Post by NicolasRobidoux »

Does lighten/darken only affect the luminance based on the luminance, or does it work channel by channel?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: halo minimization by local min/max smooth clamping

Post by fmw42 »

Image

infile="house3a.png"
inname=`convert $infile -format "%t" info:`
filt="triangle"
fact=150
convert $infile \
\( -clone 0 -filter lanczos -resize $fact% \) \
\( -clone 0 -statistic minimum 3x3 -filter $filt -resize $fact% \) \
\( -clone 0 -statistic maximum 3x3 -filter $filt -resize $fact% \) \
\( -clone 1 -clone 2 -compose lighten -composite \
-clone 3 -compose darken -composite \) \
-delete 0-3 ${inname}_${filt}_${fact}_clamplocal.png

Image

convert $infile -filter lanczos -resize ${fact}% ${inname}_lanczos_${fact}.png

Image

compare -metric rmse ${inname}_lanczos_${fact}.png ${inname}_${filt}_${fact}_clamplocal.png null:
288.598 (0.00440373)




infile="house3b.png"
inname=`convert $infile -format "%t" info:`
filt="triangle"
fact=150
convert $infile \
\( -clone 0 -filter lanczos -resize $fact% \) \
\( -clone 0 -statistic minimum 3x3 -filter $filt -resize $fact% \) \
\( -clone 0 -statistic maximum 3x3 -filter $filt -resize $fact% \) \
\( -clone 1 -clone 2 -compose lighten -composite \
-clone 3 -compose darken -composite \) \
-delete 0-3 ${inname}_${filt}_${fact}_clamplocal.png

Image

convert $infile -filter lanczos -resize ${fact}% ${inname}_lanczos_${fact}.png

Image

compare -metric rmse ${inname}_lanczos_${fact}.png ${inname}_${filt}_${fact}_clamplocal.png null:
315.3 (0.00481116)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: halo minimization by local min/max smooth clamping

Post by magick »

Which option are you talking about? There is no specific -lighten or -darken option.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: halo minimization by local min/max smooth clamping

Post by NicolasRobidoux »

magick wrote:Which option are you talking about? There is no specific -lighten or -darken option.
In Fred's code above (thank you, Fred) he uses "-compose lighten".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: halo minimization by local min/max smooth clamping

Post by fmw42 »

NicolasRobidoux wrote:Does lighten/darken only affect the luminance based on the luminance, or does it work channel by channel?
My understanding is channel by channel. But you can ask Anthony to be sure.

see http://www.imagemagick.org/Usage/compose/#lighten and the comment:


Unfortunately these operators work on each RGB channel separately, and as such this could result in some color distortion, especially near edges of highly contrasting colors.
Last edited by fmw42 on 2011-04-03T16:57:51-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: halo minimization by local min/max smooth clamping

Post by fmw42 »

magick wrote:Which option are you talking about? There is no specific -lighten or -darken option.
-compose lighten/darken -composite
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: halo minimization by local min/max smooth clamping

Post by NicolasRobidoux »

When I have a minute, I'll do tests with a larger enlargement ratio (cropping smaller some pieces of the "house" test picture, and also using the rose: picture).

With an enlargement ratio of 1.5, it is hard to see halos: They are less than one pixel wide. The RMSE makes it clear that there is a difference. But it certainly is not obviously visible.

The good news to me is that the scheme does not add obvious artifacts. Fingers crossed.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: halo minimization by local min/max smooth clamping

Post by NicolasRobidoux »

At high enlargement ratios, clamp-locally does correct some of the worst cases of over/undershoot but it occasionally gives a "pressed against the glass" look to the places where the clamping is "on" (because it creates "flat top" plateaus, at least with bilinear and hermite; when I have time I'll see if it's as noticeable with LBB). What this suggests to me is that the clamped version should not be taken straight, but "on ice" (say, 50% clamped + 50% unclamped).

Look in Lena's hair at 800%. (Elsewhere, too.)

Not too bad for a start.
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: halo minimization by local min/max smooth clamping

Post by NicolasRobidoux »

Code: Select all

convert rose: \
\( -clone 0 -filter lanczos -resize 1800% \) \
\( -clone 0 -statistic minimum 3x3 -filter hermite -resize 1800% \) \
\( -clone 0 -statistic maximum 3x3 -filter hermite -resize 1800% \) \
\( -clone 1 -clone 2 -compose lighten -composite \
-clone 3 -compose darken -composite \) \
-delete 0-3 rose_hermite_clamplocal.png

filt="triangle"
convert rose: \
\( -clone 0 -filter lanczos -resize 1800% \) \
\( -clone 0 -statistic minimum 3x3 -filter triangle -resize 1800% \) \
\( -clone 0 -statistic maximum 3x3 -filter triangle -resize 1800% \) \
\( -clone 1 -clone 2 -compose lighten -composite \
-clone 3 -compose darken -composite \) \
-delete 0-3 rose_triangle_clamplocal.png

convert rose: -filter lanczos -resize 1800% rose_lanczos.png
makes really clear what this does.

Comment: I think that this should be applied to the luminance channel, not to chrominance. (Later.)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: halo minimization by local min/max smooth clamping

Post by fmw42 »

Comment: I think that this should be applied to the luminance channel, not to chrominance. (Later.)
It is being applied to the RGB channels. If you only want it applied to the luminance channel, then what colorspace do you want to apply it to and I will set that up.

see colorspace table: http://www.imagemagick.org/script/comma ... colorspace

Do you want to convert first to Rec601YCbCr, Rec709YCbCr, YCC, or YCbCr (or HSL or HSB)?
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: halo minimization by local min/max smooth clamping

Post by NicolasRobidoux »

Pardon my ignorance Fred:

Ideally, I'd like a conversion to a linear format with a luminance (intensity) channel. Is there a "standard" one?

Do the interpolation/processing in that format, only clamping the luminance channel. Then back to sRGB, say.

Makes sense?

(I'll need to check that the conversion is not lossy because of clamping in the conversion. My oh my. So many details to check all the time. Of course, everything is safe if using floating point.)
Last edited by NicolasRobidoux on 2011-04-05T06:59:27-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: halo minimization by local min/max smooth clamping

Post by fmw42 »

NicolasRobidoux wrote:Pardon my ignorance Fred:

Ideally, I'd like a conversion to a linear format with a luminance (instensity) channel. Is there a "standard" one?

Do the interpolation/processing in that format, only clamping the luminance channel. Then back to sRGB, say.

Makes sense?

(I'll need to check that the conversion is not lossy because of clamping in the conversion. My oh my. So many details to check all the time. Of course, everything is safe if using floating point.)

That processing was what I had in mind. It was just deciding which YCbCr colorspace to use. I know little about them. I believe that one of the Rec formats ( probably Rec709) is what is preferable as a digital standard, but I would have to look into this myself.

I don't know that any of those are non-linear. You would have to look at the code to be sure.

see
http://en.wikipedia.org/wiki/YCbCr Y'CbCr is non-linear and YCbCr is linear, I believe. YCC may be non-linear in IM. see http://www.imagemagick.org/script/comma ... colorspace

I will set it up this afternoon with Rec709YCbCr, but any of the others can easily be used or tried.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: halo minimization by local min/max smooth clamping

Post by fmw42 »

Processing Luminance in Rec709YCbCr

Code: Select all

infile="rose:"
fact=1800
convert $infile -filter lanczos -resize ${fact}% rose_lanczos_${fact}.png
Triangle/Bilinear

Code: Select all

infile="rose:"
filt="triangle"
fact=1800
convert $infile -colorspace Rec709YCbCr -separate \
\( -clone 0 -filter lanczos -resize $fact% \) \
\( -clone 0 -statistic minimum 3x3 -filter $filt -resize $fact% \) \
\( -clone 0 -statistic maximum 3x3 -filter $filt -resize $fact% \) \
\( -clone 3 -clone 4 -compose lighten -composite \
-clone 5 -compose darken -composite \) \
\( -clone 1 -filter lanczos -resize $fact% \) \
\( -clone 2 -filter lanczos -resize $fact% \) \
-delete 0-5 -set colorspace Rec709YCbCr -combine \
-colorspace RGB rose_${filt}_${fact}_clamplocal_ycbcr.png

compare -metric rmse rose_lanczos_${fact}.png rose_${filt}_${fact}_clamplocal_ycbcr.png null:
150.326 (0.00229383)


Hermite:

Code: Select all

infile="rose:"
filt="hermite"
fact=1800
convert $infile -colorspace Rec709YCbCr -separate \
\( -clone 0 -filter lanczos -resize $fact% \) \
\( -clone 0 -statistic minimum 3x3 -filter $filt -resize $fact% \) \
\( -clone 0 -statistic maximum 3x3 -filter $filt -resize $fact% \) \
\( -clone 3 -clone 4 -compose lighten -composite \
-clone 5 -compose darken -composite \) \
\( -clone 1 -filter lanczos -resize $fact% \) \
\( -clone 2 -filter lanczos -resize $fact% \) \
-delete 0-5 -set colorspace Rec709YCbCr -combine \
-colorspace RGB rose_${filt}_${fact}_clamplocal_ycbcr.png

compare -metric rmse rose_lanczos_${fact}.png rose_${filt}_${fact}_clamplocal_ycbcr.png null:
151.032 (0.0023046)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: halo minimization by local min/max smooth clamping

Post by anthony »

fmw42 wrote:
NicolasRobidoux wrote:Does lighten/darken only affect the luminance based on the luminance, or does it work channel by channel?
My understanding is channel by channel. But you can ask Anthony to be sure.

see http://www.imagemagick.org/Usage/compose/#lighten and the comment:

Unfortunately these operators work on each RGB channel separately, and as such this could result in some color distortion, especially near edges of highly contrasting colors.
That is how they are working, as they are defined by the SVG composition operator reference.
When transparency is involved, color values are weighted by their alpha, and alpha is given 'over' blending (screen).

I did make a change so that if the -channel 'Sync' is turned off. EG: set -channel RGB then no alpha weighting, and alpha itself works on a 'lighten' (maximum) composition. that is 'no-sync' flag in channel, and no syncronization of channels are performed. BUT in either case it is still purely channel by channel.

I would like to add a Lighten_Intensity and Darken_Intensity option which will select the whole pixel of one image or the other based in the 'intensity' of the colors. I do this for example in morphology with the experimental intensity variants.. http://www.imagemagick.org/Usage/morphology/#intensity

This should not take much effort to add, if you want it! Takes longer to document it though!

Of course none of the above cases matter if you apply it to greyscale images.

Or even binary images. In which cans Lighten and Darken can be thought of as being Mathematical Set Operators...
http://www.imagemagick.org/Usage/compose/#set_theory


Currently the the only way to do intensity lighten, is to craete two grayscale images, lighten them together,
compare them against one of the greyscale images to generate a mask, then use that mask to select pixels on the original image.

Code: Select all

convert  image1.png image2.png \
             \( -clone 0--1 -colorspace Gray \
                 \( -clone 0--1 -compose Lighten -composite \) \
                 -delete 1 -compose Difference -composite -threshold 0 \
              \) -compose Src -composite     intensity_lightened.png
Horrible isn't it! You can get Darken simply by inverting the mask or changing -delete 1 to -delete 0

Of course how do you actually compare images with transparency. Is a semi-transparent white equivalent to
a grey?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NicolasRobidoux
Posts: 1944
Joined: 2010-08-28T11:16:00-07:00
Authentication code: 8675308
Location: Montreal, Canada

Re: halo minimization by local min/max smooth clamping

Post by NicolasRobidoux »

anthony wrote:...
I would like to add a Lighten_Intensity and Darken_Intensity option which will select the whole pixel of one image or the other based in the 'intensity' of the colors. I do this for example in morphology with the experimental intensity variants.. http://www.imagemagick.org/Usage/morphology/#intensity...
I would love this!

-----

IMNSHO ("...Not So Humble..."), the default behavior should be that the alpha channel does not contribute to the computation and is simply copied over from one the two input images (the first one, say).

(If the behavior is that the non-alpha channels and the alpha channel have the operation applied to them independently, the above can be emulated by setting the alpha channel of one of the two input images to "MINVAL" (Lighten) or "MAXVAL" (Darken), in other words by making the image completely opaque or completely transparent.)
Post Reply