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.
User avatar
fmw42
Posts: 25654
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 » 2011-04-05T13:04:54-07:00

If I understand this will do it. Not sure about the alpha issue -- it will take the alpha from whichever image is used for the other channels.

Image

Image


mode="min"
if [ "$mode" = "min" ]; then
swapping="+swap"
else
swapping=""
fi
convert zelda3.jpg lena2.jpg \
\( -clone 0 -colorspace Rec709Luma \) \
\( -clone 1 -colorspace Rec709Luma \) \
\( -clone 2 -clone 3 $swapping -compose minus -composite -threshold 0 \) \
-delete 2,3 -compose over -composite zelda_lena_$mode.jpg

Image



mode="max"
if [ "$mode" = "min" ]; then
swapping="+swap"
else
swapping=""
fi
convert zelda3.jpg lena2.jpg \
\( -clone 0 -colorspace Rec709Luma \) \
\( -clone 1 -colorspace Rec709Luma \) \
\( -clone 2 -clone 3 $swapping -compose minus -composite -threshold 0 \) \
-delete 2,3 -compose over -composite zelda_lena_$mode.jpg

Image

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 » 2011-04-05T16:47:57-07:00

Am I missing something, or your latest code still takes the min/max channel by channel?

(We want to take the min/max over the first, luminance, channel, and only modify this one.)

User avatar
fmw42
Posts: 25654
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 » 2011-04-05T16:56:40-07:00

NicolasRobidoux wrote:Am I missing something, or your latest code still takes the min/max channel by channel?
I guess I misunderstood. I took the luminance channel of each image. Found the min/max from each and made a mask of that. Then applied the mask to composite the two RGB images.
NicolasRobidoux wrote:(We want to take the min/max over the first, luminance, channel, and only modify this one.)
This does not make sense to me as then to which image do you apply the result (both?)? Or are you trying to get the min/max from a window over the luminance channel of only one image?

Please clarify. Then perhaps I can script it again differently.

Fred

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 » 2011-04-05T17:43:33-07:00

Fred:

I'm sorry. I did not see, just reading the code, that you were just doing min/max over the luminance channel, and only the luminance channel.

To make a long story short, I want to replace the luminance channel, and only the luminance channel, of the resized image by clamping it like was done before for all channels.

Working in linear light is just because generally resampling works better in linear light, but it's not necessary.

-----

I'll look at things more carefully when I've caught up on grading.

User avatar
fmw42
Posts: 25654
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 » 2011-04-05T18:45:05-07:00

To make a long story short, I want to replace the luminance channel, and only the luminance channel, of the resized image by clamping it like was done before for all channels.
That is exactly what I presented 7 messages above (see page 2, third message from bottom)

Fred

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 » 2011-04-05T19:24:16-07:00

fmw42 wrote:
To make a long story short, I want to replace the luminance channel, and only the luminance channel, of the resized image by clamping it like was done before for all channels.
That is exactly what I presented 7 messages above (see page 2, third message from bottom)

Fred
Indeed. (Sorry, I skipped a beat.)

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 » 2011-04-05T19:52:05-07:00

NicolasRobidoux wrote:
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!
See what I can do. Just adding a "Fast Distance" morphology function at the moment.
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).
I would agree. BUT that is not the SVN defined handling, at least for the existing methods. That was why I added the
'sync' flag. NOTE you can turn off alpha in both images, do the composition, then 'turn on alpha again' to restore the destaintion image alpha channel ;-) turning off does not delete alpha, just disable it from updates and saves ;-)

My question is how should an intensity varient handle alpha :-) the point of intensity varient is that the whole pixel is copied from one of the image images without any mix. But how should alpha effect the intensity for that choice. I see no clear solution! What does photoshop do? In the morphology Intensity methods I just simply ignored alpha for that choice.
(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.)
I don't follow the above.

Intensity varients by there very nature would ignore any channel settings, as it just does not make a lot of sense.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

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 » 2011-04-05T19:56:17-07:00

fmw42 wrote:If I understand this will do it. Not sure about the alpha issue -- it will take the alpha from whichever image is used for the other channels.
Yeap pixels are preserved. and when no transparency is involved. the soltuion is clear.
But it is how alpha should effect the choice of which image to extract the pixel from? That is the problem!

How do you intensity compare say a opaque gray color and semi-transparent white ? Which is lighter?
what about opaque gray and fully-transparent white?


Sorry I suppose I am side-tracking this discussion, which is on resizing.
I'll see about implementing a intensity variant.
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 » 2011-04-06T07:48:16-07:00

Fred and Anthony:

I am worried that convertion to and from an image format in which negative values are clamped up to zero would lead to accuracy issues, esp. when using low QUANTUM. The Rec... formats involve negative conversion coefficients; consequently, there could be some loss from converting to and from it. I actually suspect that some of the visible artifacts of clamp-locally in the rose enlargements come from this conversion. (Converting to a linear format introduces less conversion error.)

On the other hand, maybe clamp-locally is a good attempt, but actually not a good scheme?

Now:

Could you guys make things work by converting the input image to (ideally, linear) greyscale instead of an image formate with an intensity channel?

The only tricky thing is then to modify the intensity of the final result using the locally-clamped clamped greyscale "intensity" image.

The above comments are also relevant to Anthony's idea RE: ligthen and darken, not only clamp-locally.

I don't quite see whether this can be made to work.

(Apologies for terseness.)

------

Also: The preliminary results I get with Fred's code are promising, but suggest that this needs to be tweaked a bit if this will work well for "all" images, not only, say, line drawings. Also, at some point I'll need to see if LBB does bettter than bilinear and Hermite.

LBB (Locally Bounded Bicubic) is a novel, and fairly complicated, scheme, only appropriate for resampling and upsampling, not downsampling (unless you want to do supersampling before downsampling, which is probably a waste):

https://github.com/jcupitt/libvips/blob ... le/lbb.cpp

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 » 2011-04-06T08:18:36-07:00

It is starting to look like this: It's going to take a fair amount of work/tweaking to make clamp-locally work "just right" in "most" situations, or at least making sure that it works "just right" (assuming it ever gets the "Recommended for casual users" sticker).

Example: I'm not even sure anymore than going through the intensity is the right thing to do. And I'm starting to think that triangle is just about as good as more sophisticated schemes.

On the good news front, I think that I have finally figured out how to deal with downsampling.

Manana.

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 » 2011-04-06T16:26:06-07:00

Anthony and Fred:

I think I figured out how to do this with a greyscale version of the color image, instead of one converted to an image format with an "intensity" channel.

More about this (much) later.
Last edited by NicolasRobidoux on 2011-04-06T18:34:04-07:00, edited 1 time in total.

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 » 2011-04-06T16:40:42-07:00

This is very preliminary because I've only looked carefully at the rose image, but I suspect that clamp-locally is a "close but no cigar" method.

User avatar
fmw42
Posts: 25654
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 » 2011-04-06T17:26:52-07:00

NicolasRobidoux wrote:Anthony and Fred:

I think I figured out how to do this with a greyscale version of the color image, instead of one converted to an image formate with an "intensity" channel.

More about this (much) later.

I look forward to hearing more when you get the time.

Fred

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 » 2011-04-07T05:36:34-07:00

The Intensity variants of Lighten and Darken as now been added to the SVN, and will be published in IM v 6.6.9-5

Examples of its use are in IM Usage (which takes longer to update than the actual coding).
http://www.imagemagick.org/Usage/compos ... _intensity

It has two modes of operation, a default 'alpha-weighted intensity' whole pixel selection mode,
or a 'no-weighting' mode but allowing you to just transfer specific (or 'ALL') channels.

That second mode makes it relatively easy to generate a 'shaped mask' using alpha channel coping, if a mask is all you want.

You could even use it as a funny type of "Select this alpha on this, otherwise select that alpha" type of selection.

I am still looking for a more formal definition of "Lighten by Intensity", as it is not covered by the normal SVG or Photoshop definitions I use for composition methods.
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 » 2011-04-07T08:27:15-07:00

Anthony:

Good job!

It's really clear, intuitive, and useful.

If you don't want to add any new colour, you cannot have that the operation does not depend on the order of the input images, because resolving ties (same "greyscale" intensity in both images at some pixel) cannot be resolved symmetrically.

Solution 1: If there is a tie, you take the average of the two pixel values. This, of course, will introduce new colours.

Solution 2: If there is a tie, you always pick the pixel value from the first input image. This will not introduce new colours, but it will break the symmetry w.r.t. the two inputs.

(If I run into a "standard" defition, I'll let you know.)

Post Reply