Page 1 of 1

Adding/Subtracting Two Images

Posted: 2015-09-25T05:23:34-07:00
by coolbeans201
Hi all,

I'm new to ImageMagick as I have just started working on an image analysis project. I've working with some existing, but old, code for working with these images. I'm trying to understand how to add and subtract images as a test run with the code. Before, it looked like this:

Code: Select all

                        if (chosen_arithmetic == ADD)
			{
                            int status = execl("/usr/local/bin/combine", "combine", "-compose", "add", "temp_in1.pgm", "temp_in2.pgm", "temp_out.pgm", (char*)NULL);
			    if(status == -1) //error executing
			    {
					printf("Error executing.\n");
					exit(1);
			    }
			}
                        else if (chosen_arithmetic == SUBTRACT)
			{
                             int status = execl("/usr/local/bin/combine", "combine", "-compose", "subtract", "temp_in1.pgm", "temp_in2.pgm", "temp_out.pgm", (char*)NULL);
			     if(status == -1) //error executing
			     {
					printf("Error executing.\n");
					exit(1);
			     }
			}
I am assuming this no longer works due to newer versions being released since it was last worked on, but I want to know what I need to do now to have the same effect. Any ideas? Thanks so much!

Re: Adding/Subtracting Two Images

Posted: 2015-09-25T06:19:27-07:00
by snibgo
Is that ImageMagick? If so, it must be very old. What does "combine -version" say?

The current commands would be:

Code: Select all

convert temp_in1.pgm temp_in2.pgm -compose Plus -composite temp_out.pgm

convert temp_in1.pgm temp_in2.pgm -compose MinusSrc -composite temp_out.pgm
But the "MinusSrc" may need to be "MinusDst".

Re: Adding/Subtracting Two Images

Posted: 2015-09-25T06:31:38-07:00
by coolbeans201
Yeah, this was last worked on 10 years ago. And I figured it out too. Just a silly logical error. Thanks!

Re: Adding/Subtracting Two Images

Posted: 2015-09-25T06:37:37-07:00
by coolbeans201
Also, what's the difference between what you have and this?

Code: Select all

int status = execl("/usr/bin/composite", "composite", "temp_in1.pgm", "temp_in2.pgm", "-compose", "plus", "temp_out.pgm", (char*)NULL);

Code: Select all

int status = execl("/usr/bin/composite", "composite", "temp_in1.pgm", "temp_in2.pgm", "-compose", "subtract", "temp_out.pgm", (char*)NULL);
I've seen how you can use either convert or composite for image manipulation, but is there any significant difference? Thanks again!

Re: Adding/Subtracting Two Images

Posted: 2015-09-25T07:27:25-07:00
by snibgo
coolbeans201 wrote:Also, what's the difference between what you have and this?
Your code is part of a C-program (or something). Mine is a command typed at the console. Was that the question?

In the old days, "composite" was used for one job, and "convert" was used for a different job. Since then, "convert" has gained almost all of the functionality of "composite". Very nearly everything that "composite", "convert" can also do, but it can also do much more.

I wouldn't use "composite" unless I had to. With "convert", we write the command in the logical, chronological order: read images, process images, write images.

One important difference: when these take two inputs, they are specified in the opposite order. So the following do the same:

Code: Select all

convert a.png b.png -compose Plus -composite out.png

composite b.png a.png -compose Plus out.png

Re: Adding/Subtracting Two Images

Posted: 2015-09-25T08:40:54-07:00
by coolbeans201
My question was more along the lines of the difference between add, Plus, subtract, and MinusSrc. Are they the same for the operations they perform? I want to keep my code as similar to the functionality that existed before, so want to choose the right one.

Re: Adding/Subtracting Two Images

Posted: 2015-09-25T09:23:19-07:00
by snibgo
Sorry, I don't know what the "combine" program does or did. If it still works, you'll need to test it.

For the current "-compose" options, see http://www.imagemagick.org/Usage/compose/