How do I simplify this mess ? (merging/stacking many images)

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?".
Werty
Posts: 68
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

How do I simplify this mess ? (merging/stacking many images)

Post by Werty » 2010-08-06T07:45:00-07:00

Hi.

I'm capturing alot of screenshots and compare them with the -compare command (IMobject.dll), then stack the results ontop of eachother, in a "grayscalish" way.

It's works fine and I'm happy, but I could be even happier if someone could help me getting it all into one command only, as that should easily be possible, but dunno how to do it.

What it does....

takes hg1.png and compare it with hg2.png, and spit out the difference in 16bit pgm as diff.pgm (notice the color change every loop)
then they are all stacked using -compose darken, as result.pgm (that overwrites itself all the time, intended and works good)

Code: Select all

Func stackimages()
	
	$compcolor = ($min-$min)+1
	$canvas = $img.Convert("-size", "512x512", "xc:white", "-depth", "16", "-compress", "none", "g:/temp/result.pgm")
	For $height = $min to $max
				
		 ;---------------screen area gets captured here and saved as "hg1.png"-----------------
		
		
		;-----------stuff happens here that changes the screen area----------------
		
                                ;---------------same screen area gets captured here and saved as "hg2.png"-----------------
		
$compare = $img.Compare("g:/temp/hg1.png", _
						"g:/temp/hg2.png", _
						"-depth", "16", _
						"-compress", "none", _
						"-compose", "Src", _
						"-highlight-color", "#" & Hex(($compcolor+1)*64,4) & Hex(($compcolor+1)*64,4) & Hex(($compcolor+1)*64,4), _
						"g:/temp/diff.pgm" )
						
$composite = $img.Composite("-depth", "16", _
							"-compress", "none", _
							"-compose", "Darken", _
							"g:/temp/result.pgm", _
							"g:/temp/diff.pgm", _
							"g:/temp/result.pgm" )
						$compcolor = $compcolor+1				
Next				
	
EndFunc
Now, looking at it, it should be obvious that it can be done better, if you look at the loop then you can see that it takes the same screenshot twice, that shouldnt be necessary, and the stacking part looks like it could be done with a simple single command.

Could someone help me with this please ?

Werty
Windows 7 user

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

Re: How do I simplify this mess ? (merging/stacking many ima

Post by fmw42 » 2010-08-06T09:56:30-07:00

I have not looked at this in much detail, but since compare, composite and convert are all different IM functions, I doubt it can be put into one single command line in any reasonable manner without using pipes and such. Scripting seems like the best approach.

Werty
Posts: 68
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How do I simplify this mess ? (merging/stacking many ima

Post by Werty » 2010-08-06T10:18:04-07:00

fmw42 wrote:I have not looked at this in much detail, but since compare, composite and convert are all different IM functions, I doubt it can be put into one single command line in any reasonable manner without using pipes and such. Scripting seems like the best approach.
I'm pretty sure it can be done, I've been close several times the past couple of days I've been attempting it.

But where I have been close, almost sure I got it, the problem was I couldnt add -highlight-color to the convert "-compose -composite" combinations I tried, crashed everytime I tried to add it.

I would also like to know how I can use image blobs with imagemagickobject.dll, when capturing a screenshot with WinAPI I would like to feed it directly to an IM command instead of saving to disk first.
Windows 7 user

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

Re: How do I simplify this mess ? (merging/stacking many ima

Post by fmw42 » 2010-08-06T10:27:31-07:00

Sorry, I did not look close enough at your code, but you are using an API. Sorry, I cannot answer about any API programming to use blobs, all I know is scripting the command line, where you cannot mix and match compare, convert, composite in the same command line.

What version of IM are you using? If old, perhaps upgrade.

Werty
Posts: 68
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How do I simplify this mess ? (merging/stacking many ima

Post by Werty » 2010-08-06T10:32:45-07:00

I'm using 6.6.3.Q16, downloaded it recently, I have just started using IM, it's pretty cool. :)
Windows 7 user

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

Re: How do I simplify this mess ? (merging/stacking many ima

Post by fmw42 » 2010-08-06T10:42:03-07:00

Just one point as I see you are using -compose darken. If you are trying to composite more than two images with -compose darken at the same time, then you need to use -layers merge and not -composite. -composite only works with two images at a time for a given -compose method. Sorry I don't know the equivalent in your API

Werty
Posts: 68
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How do I simplify this mess ? (merging/stacking many ima

Post by Werty » 2010-08-06T11:26:53-07:00

Thanks, I'll look into the layer stuff.

And dont mind the api stuff, I would be happy if you posted Unix command lines, same thing anyway. :)
Windows 7 user

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

Re: How do I simplify this mess ? (merging/stacking many ima

Post by fmw42 » 2010-08-06T11:44:14-07:00

Werty wrote:Thanks, I'll look into the layer stuff.

And dont mind the api stuff, I would be happy if you posted Unix command lines, same thing anyway. :)

If you want me to look into a command line equivalent, please give a more detailed step by step outline of your processing (what you want to accomplish by each step in your concept). And provide a small set of example images (links to them) if possible.

Werty
Posts: 68
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How do I simplify this mess ? (merging/stacking many ima

Post by Werty » 2010-08-06T13:46:10-07:00

Right, imagine you have this colorful cone...

Image

...then a piece of black paper is put over it, and a photo (screenshot in my case) is taken from straight above.

Then the paper is moved a bit upwards, and another pic is taken, all from straight above, that is repeated till the cone is fully covered (all black, no colors left), until you end up with a whole bunch of them.

Then I need them stacked this way, starting with the bottom one and putting the following ontop, but in a lighter shade of gray (defined by me as in the example in my first post), all that is black should stay black, all that is equal should be removed or ignored (they would be covered later anyway as the paper moves up), so just the difference get's a new color.

As shown in the pic in the corner, I want to "lift" off a gradient from the cone.

All pics are always 512x512, and the pics are static, same pic all the time, just with that "paper effect" that changes them a bit, which then should be compared, and stacked.

I could really do without all that file reading/writing, it's ~500-1000 pics in a go, must be done better :)

Hope I was clear enough, else just ask.
Last edited by Werty on 2010-08-06T14:10:05-07:00, edited 1 time in total.
Windows 7 user

Werty
Posts: 68
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How do I simplify this mess ? (merging/stacking many ima

Post by Werty » 2010-08-06T13:57:59-07:00

Here's an example made with the code in my first post...

Image
Windows 7 user

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

Re: How do I simplify this mess ? (merging/stacking many ima

Post by fmw42 » 2010-08-06T14:10:23-07:00

This image was made from the cone image or something else. If something else, I have no idea what to expect!

On this image, just apply -auto-level on the result to bring out the details. But again I have no idea what to expect as I don't know what the input image looks like.

Can you provide the input image and some formula for how you change the graylevel between steps.

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

Re: How do I simplify this mess ? (merging/stacking many ima

Post by fmw42 » 2010-08-06T14:26:44-07:00

So you are taking slices of a 3D object. Is that correct? Then trying to overlay/composite them (stack them) to get the difference area slightly lightened as you go up? Please correct me as I am not sure I understand. If all the slices are from the same image, there will be no differences except in the outer rings where one image is black and the other is not. But as you build this up, it would seem to me that you will end up with the same image ( or the negative of the image) you started with. I guess I am missing something.

-compose darken on a pair of images will take the darker of the two images on a pixel by pixel basis. Is this what you want to do?

Sorry can you explain again what processing steps you want to do on the previous iterated result with the next slice and what you expect to happen as a result (i.e. what it would look like)

Also if you could provide a small set of slices (say no more than about 6) as individual images, that would be helpful so that others can try to do this (for me in command line) to work out the procedure. Your example of the cone would be OK if you just provide the images separately. I presume the ring image in the lower left corner is what you want to result? Is that correct? But there is no texture from the input images. So I presume you are just looking to identify where they are different and not by how much on pixel-by-pixel basis. Is that correct?

If you are doing this on hundreds if not more slices, then this is going to be slow no matter what you do unless you can hold all the input images in memory so that you can use in memory storage format .mpr to contain the result of each step.

Werty
Posts: 68
Joined: 2010-08-06T05:37:36-07:00
Authentication code: 8675308

Re: How do I simplify this mess ? (merging/stacking many ima

Post by Werty » 2010-08-06T14:37:14-07:00

As mentioned, I already accomplished what I want, as you also can see in the pic I may have posted while you wrote.

What I'm looking for is to get my code refined so it doesnt use so much disk, a more refined way of doing it, with less disk activity.

Here's the commands from my first post that already does it...

Compare("g:/temp/hg1.png", _
"g:/temp/hg2.png", _
"-depth", "16", _
"-compress", "none", _
"-compose", "Src", _
"-highlight-color", "#" & Hex(($compcolor+1)*64,4) & Hex(($compcolor+1)*64,4) & Hex(($compcolor+1)*64,4), _
"g:/temp/diff.pgm" )

Composite("-depth", "16", _
"-compress", "none", _
"-compose", "Darken", _
"g:/temp/result.pgm", _
"g:/temp/diff.pgm", _
"g:/temp/result.pgm" )
$compcolor = $compcolor+1[/code]

I'm just trying to find another better method :)
Windows 7 user

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

Re: How do I simplify this mess ? (merging/stacking many ima

Post by fmw42 » 2010-08-06T14:49:02-07:00

What I was saying is that I don't code API so I cannot figure out what you are doing without more detailed help from you and examples that I can play with. If I knew exactly what you were doing, I could probably work it out in command line, and perhaps improve on it. But then someone would have to help you convert it back to your API.

So, I guess you will need help from someone who knows the IM APIs.

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

Re: How do I simplify this mess ? (merging/stacking many ima

Post by fmw42 » 2010-08-06T15:05:45-07:00

The only thing I can suggest from my limited understanding of your API code might be to avoid the compare and use convert with -compose difference. Then use convert also for your composite -compose darken so that you can do them both in the same convert command line without having to write out a temp file between the two operations.

Post Reply