parenthesis with compose Plus and Minus

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?".
haarts

parenthesis with compose Plus and Minus

Post by haarts »

Hi,
I'm trying to generate a heatmap. Catch in this heatmap is that some points have a positive influence and others have a positive influence. I have an image with a basic point (posdot14.png, 38x38 pixels). The center pixel is white and becomes progressively blacker towards the borders.

Code: Select all

convert -page 256x256 -background black \( -page +31+31 posdot14.png -compose Plus \) \( -page +51+31 posdot14.png -compose Minus \) -composite /tmp/out.png
This however yields an image of 38x38 pixels instead of the 256x256 I expected. Also is my approach of alternating -compose Plus and -compose Minus with the -composite suffix correct?

Thanks for any help.

With kind regards,
Harm
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: parenthesis with compose Plus and Minus

Post by anthony »

-page sets the size and offset of images on the virtual canvas. It does not actually create a image of that size! That is why it is called 'virtual'. It is a larger 'pretend' canvas used for "image layering" techniques.

Also virtual canvas and offset are not used by the lower level -composite operation. -geometry and -gravity is used for that!

See IM Examples, Image Composition, Positioning The Overlay Image
http://www.imagemagick.org/Usage/compose/#geometry

If you want to use -page use -flatten instead - don't forget to specify a -background color such as 'none' or 'transparent'.

For various ways of laying multiple images together see IM Examples, Layering images
http://www.imagemagick.org/Usage/layers/

This whole section covers things like.... appending, drawing, compositions, flattening, and more advance methods of mosaics, and layer merging.
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: parenthesis with compose Plus and Minus

Post by anthony »

I would like to know more about what you are actually attempting to do.
Heat images are usually a form of diffusion image, showing a gradient representing the equalised heat distribution.

If so you may like to look at Shepards Sparse Color, and the later 'faster' methods of global diffusion.
http://www.imagemagick.org/Usage/canvas/#sparse_fill

Note this does not have a understanding of 'boundaries', as such diffused colors can 'leak' across the fixed pixels.

I hope to implement true diffusion (with correct boundary handling) at some point, but I have no ETA on this.

It will probably be based on a technique using a Voronoi color fill as the start for color seeding, and the Morphology Distance method, as outlined by the paper on Diffusion Curves, to define the very very simplified variable blurring needed at each point.
http://artis.imag.fr/Publications/2008/OBWBTS08/

As you can see I have this on my mind!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
haarts

Re: parenthesis with compose Plus and Minus

Post by haarts »

Thank you very much for you reply. The Sparse Color operator certainly looks relevant. But first I would like to understand the parenthesis better as I still don't quite grasp the implications of the concept together with -composite.
As you pointed out -page is virtual and I should have known. My second stab at the problem:

Code: Select all

convert -size 256x256 xc:black \( -geometry +31+31 posdot14.png -compose Plus \) \( -geometry +171+31 posdot14.png -compose Plus \) -composite /tmp/out.png
I struggling with the concept of Aplha composition. This works as expected:

Code: Select all

convert -size 256x256 xc:black posdot14.png -geometry +171+31  -compose Plus -composite /tmp/out.png
This however seems to ignore the compose Plus but does however show two points:

Code: Select all

convert -size 256x256 xc:black \( posdot14.png -geometry +51+31 -compose Plus \) -composite \( posdot14.png -geometry +31+31 -compose Plus \) -composite /tmp/out.png
Looking at http://www.imagemagick.org/Usage/basics/#complex (first example) let me to believe that this would be correct:

Code: Select all

convert -size 256x256 xc:black \( +clone posdot14.png -geometry +51+31 -compose Plus -composite \) \( +clone posdot14.png -geometry +31+31 -compose Plus -composite \)  /tmp/out.png
The resulting image is black unfortunately. I must horribly misunderstand parenthesis and compose.

For reference the posdot14.png (http://img.skitch.com/20101001-83ycdmy8 ... g276ex.jpg)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: parenthesis with compose Plus and Minus

Post by Bonzo »

The parenthesis tend to be for working on an image without effecting the rest of the image something like:

Code: Select all

Clone
<?php 
$cmd = "$input \( +clone -thumbnail x480 -write 480_wide.jpg +delete \) -thumbnail 64x64! ";
exec("convert $cmd clone.jpg");
?> 
I would have thought in your case they would not be required.
haarts

Re: parenthesis with compose Plus and Minus

Post by haarts »

Sure enough removing the parentheses yield some sort of a result:

Code: Select all

convert -size 256x256 xc:black posdot14.png -geometry +51+31 -compose Plus -composite posdot14.png -geometry +31+31 -compose Plus -composite /tmp/out.png
The result can be seen here: http://img.skitch.com/20101001-g2fic8sr ... t4aagw.jpg
I doubt the compose Plus works as I expected. When placing another point things get weirder still http://img.skitch.com/20101001-4pp14m5k ... st9e3n.jpg
The code ran:

Code: Select all

convert -size 256x256 xc:black posdot14.png -geometry +51+31 -compose Plus -composite posdot14.png -geometry +51+31 -compose Plus -composite posdot14.png -geometry +31+31 -compose Plus -composite /tmp/out.png
Image with -compose Minus:
Image
And the code:

Code: Select all

convert -size 256x256 xc:black posdot14.png -geometry +51+31 -compose Plus -composite posdot14.png -geometry +31+31 -compose Minus -composite /tmp/out.png
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: parenthesis with compose Plus and Minus

Post by Bonzo »

I am usless with compose :(

I have some examples here but they are not the best: http://www.rubblewebs.co.uk/imagemagick ... ompose.php
haarts

Re: parenthesis with compose Plus and Minus

Post by haarts »

It has something to do with the order in which the several composite commands are run. I'd expected it to be a + b - c. I'm sure, however, this is not how it works. It seems c - (a + b) or c - ( b + c).
According to this thread:
viewtopic.php?f=3&t=14515&p=50538&hilit ... der#p50538
it is the latter.

I'm still struggling how to achieve the effect I desire.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: parenthesis with compose Plus and Minus

Post by fmw42 »

when using convert -compose ... -composite, the order of the images is reversed from composite -compose ...

So if you want to think about images as mathematical operations A - C would be

composite imageA ImageC -compose minus result

or

convert imageA ImageC +swap -compose minus -composite result

The +swap reverses the order of A and C as is needed for convert, but not for composite.

(Note +swap is needed for minus and divide, but not for plus or multiply as the order is not important for the latter two but very important for the former two. However, using for all does not hurt)


See http://www.imagemagick.org/Usage/compose/#compose


If you want A + B - C, then

convert imageA imageB -compose plus -composite imageC +swap -compose minus -composite result

or if you want to use parens, that is ok, but not needed

convert \( imageA imageB -compose plus -composite \) imageC +swap -compose minus -composite result
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: parenthesis with compose Plus and Minus

Post by anthony »

The compose image aorder poroblem is inherited from the concept of Duff-Porter composition where

overlay method background => result

However mathematical composition really should be of the form..

background operation overlay => result

But that is NOT what it is currently doing.

The problem with using +swap is that the 'background' image is the one that sets the final image size of the result. As such swapping images using a smaller 'overlay', and -geomtry offsets, all becomes much more difficult.


I am thinking about reversing this order but it would break a lot of existing IM scripts to make the change! Arrrggghhh....

As for you actual problem... does posdot14.png contain ALPHA?

Can we have a copy of the image so we can duplicate your actual command?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
haarts

Re: parenthesis with compose Plus and Minus

Post by haarts »

This is all starting to be a bit complex. :)
The posdot14.png image does not contains any alpha. Just greyscale. The original is found here: http://staging.delaagsterekening.nl/images/posdot14.png
Currently I'm trying to find a way to achieve the desired result with the IM API (via RMagick). I'm still working on the problem and I'm investigating the -average option with layers. I AM using transparent pixels in that operation as I was hoping those would be ignored. But it seems transparency is interpreted as black (=0), which makes sense. I'm thus going to give the Plus and Minus compose operators an other go.

Thanks for the help so far!
haarts

Re: parenthesis with compose Plus and Minus

Post by haarts »

I can't wrap my head around it. I now have two images, one I used Plus the other Minus. The result is:
Image
I was expecting the border on which the circles touch to be a straight horizontal line with the background color. They should have cancelled each other out. Instead you see the two circles in full, overlapping. I have used a slightly different dot image by the way, the original is here: http://img.skitch.com/20101003-fm735k6m ... p1d93w.png

The relevant (Ruby) code:

Code: Select all

i = Image.new(256,256) {self.background_color = 'rgb(128,128,128)'}
posdot = Image.read("/Users/harm/Desktop/posgreyblackdot14.png")[0]
negdot = Image.read("/Users/harm/Desktop/posgreyblackdot14.png")[0]
negdot.background_color = "none"
negdot = negdot.extent(256,256,127,157)
r = r.composite(posdot, 127, 127, PlusCompositeOp)
r = negdot.composite(r, 0, 0, MinusCompositeOp)
And the docs on the composite method: http://studio.imagemagick.org/RMagick/d ... #composite
haarts

Re: parenthesis with compose Plus and Minus

Post by haarts »

I now realize that is crazy talk. Any hints how I could get that effect? (I mean to get something like a Gabor? http://www.icn.ucl.ac.uk/courses/MATLAB ... orial.html At least I think that is what I need to create the heatmap)
nessence

Re: parenthesis with compose Plus and Minus

Post by nessence »

Why did you composite your canvas over the dot, for minus operation?
Shouldn't you have composite the dot over canvas?

The operation will be different.

Also, for what you are trying to do, why would you want to use a Minus operation?

I got this to work using a white canvas and Plus operation.

The way I made the dot was important. The gradience shouldn't be as drastic as white-to-black (or vice versa) because when any two pixels within half the radius of two "dots" intersect, the result will be white.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: parenthesis with compose Plus and Minus

Post by anthony »

OKAY because of the canvas' problem "minus" is not going to work, unless you 'fill out the canvas' first. -- Oh the Pain! The Pain -- Dr Zackery Smith, Lost in Space

Sorry... Any way. The photoshop method of going minus, 'Linear Burn' does not however require the image swapped, but you need to compose a 'negated' version of the image to be subtracted.
http://www.imagemagick.org/Usage/compose/#linearburn

So lets do this.

First a gray scale canvas, then add a dot, the remove a dot (as previously given by "haarts")

Code: Select all

  convert -size 69x42 xc:gray50 \
              dot.png -geometry +5+2  -compose Plus  -composite \
              \( dot.png -negate \) -geometry +26+2 -compose LinearBurn -composite \
              -scale 500%  two_dots.png
Image
The parenthesis is used to limit the negation to just the 'negative' dot image for a 'Linear Burn' composition (photoshop minus composition)
http://www.imagemagick.org/Usage/basics/#parenthesis

The resulting image shows a gradient which is linear straight (vertically) between the dot centers.

If you add then place the + and - dots at the same location, you get just the one solid color image.

Code: Select all

  convert -size 42x42 xc:gray50 \
              dot.png -geometry +2+2  -compose Plus  -composite \
              \( dot.png -negate \) -geometry +2+2 -compose LinearBurn -composite \
              -unique-colors   txt:-
# ImageMagick pixel enumeration: 1,1,65535,rgb
0,0: (32639,32639,32639) #7F7F7F7F7F7F grey50
See just one color in the resulting image!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply