"Winner" magnitude composite op ?

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
dproc
Posts: 19
Joined: 2010-09-14T20:39:15-07:00
Authentication code: 8675308

"Winner" magnitude composite op ?

Post by dproc »

I suppose I want some sort of composite operation to do this, but I'll explain it in more general terms in case the solution doesn't involve compositing:

I want to copy a pixel channel magnitude from the source image to the corresponding pixel in the destination image only if that magnitude is greater.

For example:

If a pixel in the destination image has a red magnitude of 132 and the corresponding pixel in the source image has a red magnitude of 108, then the destination pixel is not changed.

If a pixel in the destination image has a red magnitude of 132 and the corresponding pixel in the source image has a red magnitude of 149, then the destination pixel's red magnitude is set to 149.

Is there any way to do this without processing each pixel?

I'm working with Magick++ but if someone knows how to do this with the command line tools then I might figure out how to do it in Magick++

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

Re: "Winner" magnitude composite op ?

Post by fmw42 »

By magnitude, I think you really mean intensity or brightness for a given channel - correct? (Not a combination of channels, right?)

If that is correct, then see:

convert image1 image2 -compose lighten -composite result

will use the brighter of the two images on a pixel by pixel basis for each channel of the two source images creating a third new image.

or if more than two images, you can use

convert image1 image2 .... imageN -compose lighten -flatten result

or

convert image1 image2 .... imageN -evaluate-sequence max result


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

I think what you want is a variation of the above:

convert image1 image2 -compose lighten -composite image2

where image1 will have its pixel replace that in image2 if it is larger than the value in image2. So you are writing over image 2.


Sorry I don't know the IM APIs.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: "Winner" magnitude composite op ?

Post by anthony »

fmw42 wrote:

Code: Select all

convert image1 image2 -compose lighten -composite result
will use the brighter of the two images on a pixel by pixel basis for each channel of the two source images creating a third new image.
Actually it is on a VALUE by VALUE bases.

There is not currently a PIXEL by PIXEL version, selecting on the overall intensity of the whole pixel, resulting in the copy of the whole pixel.

The current work around is to do the compare of gray scale versions, then compare (say using ChangeMask)that result against one of the original greyscale images to generate a mask, to determine which pixel should come from which image. that is actually quite an involved process.

See the note in IM Examples on this compose method, about the various Photoshop version implementations of this.
http://www.imagemagick.org/Usage/compose/#lighten


Morphology operators 'dilate' and 'erode' work in a similar way, with VALUES, but I have included experiment 'Intensity' variant, that dilate and erode on the overall intensity of a pixel, the selected pixel is then copied, as a whole.
See http://www.imagemagick.org/Usage/morphology/#intensity

A Lighten_Intensity and Darken_Intensity composition method has NOT been added yet.
However if people want a intensity variant it will not be very hard to add it.

NOTE intensity variant methods will ignore any '-channel' setting completely, by its very nature.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: "Winner" magnitude composite op ?

Post by fmw42 »

anthony wrote:Actually it is on a VALUE by VALUE bases.

There is not currently a PIXEL by PIXEL version, selecting on the overall intensity of the whole pixel, resulting in the copy of the whole pixel.
This is a very confusing statement as -compose lighten does work by comparing each corresponding pixels (in a given channel as I said above) and chooses the one with the brightest value. This is my understanding. If that is not what it does, then I am very confused and need more explanation of the difference between value-by-value and pixel-by-pixel.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: "Winner" magnitude composite op ?

Post by anthony »

value by value compares just the values within a given channel and selects the larger (ligthest) value.

That means for a specific pixel composition you could get the red color value from one image and blue color value from another image producing a color that is different from BOTH input images!

It is actually rarely visible in practical cases but does happen!

A pixel by pixel method compares the intensity brightness of the whole pixel, then copies all the values from just one of the input images. That is the whole pixel will ceom of just one image. You will never get any color mismatch, and the result will only contain actual color found in the original source image.

You could call this a "Lighten by Value" or a "Lighten by Intensity"

The current "Lighten" is the former (by value), which appears to be the normal implemented method.
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: "Winner" magnitude composite op ?

Post by anthony »

dproc wrote:I want to copy a pixel channel magnitude from the source image to the corresponding pixel in the destination image only if that magnitude is greater.
I think what we really need is YOUR exact definition of 'Magnitude', though it looks like "Lighten" composition will do what you want.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
dproc
Posts: 19
Joined: 2010-09-14T20:39:15-07:00
Authentication code: 8675308

Re: "Winner" magnitude composite op ?

Post by dproc »

Yep, my bad. The word "magnitude" can be interpreted as a vector magnitude in a 3D color space, yet I didn't mean that. I subconsciously picked that word because of my application.

Now to be more clear (I hope):

It actually doesn't matter to me whether its just a channel, the magnitude of a vector in a 3D color space, or the total of all three channels. I'm really just using one channel and don't mind setting the other channels to all zeroes or working with greyscales. So I think what y'all have written above is a great help. I have the 'convert' source code but had trouble figuring out where it does the abovementioned operations. There are some undocumented composite operators in the Magick++ docs, so I may spend some time experimenting with them. Or since the pixel access function isn't too slow (Magick::Image::pixelColor) I guess I'll perform the operation in my app code, or I'll figure out how (and how fast) it is to access the pixel buffer directly.

Thanks for the great help!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: "Winner" magnitude composite op ?

Post by anthony »

All the compositing methods (well the few that aren't are just mirrors of ones that are) is documented in a practical way in IM Examples, Composition, to which I have already pointed to for "Lighten"

Actually most are a standard composition well documentin in the SVG format from the W3 consortium. ImageMagick follows that standard.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply