Multple crops, multiple composites

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
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Multple crops, multiple composites

Post by jaffamuffin »

Hi all, new here

Some great info on here, but is there a search function for the forum?

I have used imagemagick for a few years now and it has been extremely useful.

My current problem is one of speed.

I have a form, from which some regions are cropped out.
These regions are processed with another peice of software (to dilate, and erode, and some noise removal)
Then I have to put the now processed cropped sections back onto the orginal image.

Using Winxp here is the relevent part of the script:

%1 is the path, %2 is the image file name(document number), T1 and T2 are just temp files.

Code: Select all

convert "%~2_1.tif" -crop "165x375+85+1350" "%~1\Q1.tif"
convert "%~2_1.tif" -crop "165x605+85+2370" "%~1\Q2.tif"
convert "%~2_1.tif" -crop "165x865+1285+960" "%~1\Q3.tif"
convert "%~2_1.tif" -crop "165x425+1285+2400" "%~1\Q4.tif"
convert "%~2_1.tif" -crop "165x400+1285+2975" "%~1\Q5.tif"

composite -geometry +85+1350 "%~1\Q1.tif" "%~2_1.tif" "%~1\T1.tif"
composite -geometry +85+2370 "%~1\Q2.tif"  "%~1\T1.tif" "%~1\T2.tif"
composite -geometry +1285+960 "%~1\Q3.tif" "%~1\T2.tif" "%~1\T1.tif" 
composite -geometry +1285+2400 "%~1\Q4.tif" "%~1\T1.tif" "%~1\T2.tif" 
composite -geometry +1285+2975 "%~1\Q5.tif" "%~1\T2.tif" "%~1\T1.tif"
convert "%~1\T1.tif" -compress group4 -define quantum:polarity=min-is-white "%~2_1.tif"
This works, and there 8 crops for the rear image (%~2_2) so in total this takes a while I guess due to the many disk ops.

Is there some way to have Imagemagick read the image once and perform many crops, and subsequently read all the cropped areas and write them out to a file once?

Thanks all
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Multple crops, multiple composites

Post by Bonzo »

This is a piece of code I tried; it reads the image into memory in this case calls it "image". Then it saves multiple different size images and one grayscale one as it goes.

Code: Select all

convert input.jpg -write mpr:image +delete ( mpr:image -thumbnail x480 -write 480_wide.jpg ) ( mpr:image -thumbnail x250 -write 250_wide.jpg ) ( mpr:image -thumbnail x100 -write 100_wide.jpg ) ( mpr:image -thumbnail 64x64! -write 64_square.jpg ) ( mpr:image -colorspace Gray -write black_white.jpg
 
This may save a bit of time for the cropping; you could use "layers" and save them to the final image as you go?
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: Multple crops, multiple composites

Post by jaffamuffin »

thanks for the reply -

OK i did this

Code: Select all

convert %1 -write mpr:image +delete ( mpr:image -crop "165x375+85+1350" -write Q1.tif ) ( mpr:image -crop "165x605+85+2370" -write Q2.tif ) ( mpr:image -crop "165x865+1285+960" -write Q3.tif ) ( mpr:image -crop "165x425+1285+2400" -write Q4.tif ) ( mpr:image -crop "165x400+1285+2975" -write Q5.tif
And Imagemagick complains about "convert: option requires an argument `-write'" and I don't get Image Q5.tif

If I add a closing bracket on to the above I do get the Q5.tif and this error : "convert: unbalanced parenthesis `)'."

This way takes about 50-60 hundredths of a second.

The old way takes about 85-90 hundredths of a sec (using %time% before and after in a test script)

So faster.

Now for the recompositing....
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Multple crops, multiple composites

Post by Bonzo »

Sorry about the ( I would remove the ( rather than add a )

Code: Select all

convert %1 -write mpr:image +delete ( mpr:image -crop "165x375+85+1350" -write Q1.tif ) ( mpr:image -crop "165x605+85+2370" -write Q2.tif ) ( mpr:image -crop "165x865+1285+960" -write Q3.tif ) ( mpr:image -crop "165x425+1285+2400" -write Q4.tif ) mpr:image -crop "165x400+1285+2975" -write Q5.tif
This is some other code I wrote with help from others; it uses the layers but I can not remember fully how it worked now ! The old memory is failing fast and to be honist I dont have a need for most of the code I try and so its used once only.
http://www.rubblewebs.co.uk/imagemagick ... ground.jpg
http://www.rubblewebs.co.uk/imagemagick ... mbined.jpg
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Multple crops, multiple composites

Post by anthony »

Code: Select all

convert "%~2_1.tif" -crop "165x375+85+1350" "%~1\Q1.tif"
convert "%~2_1.tif" -crop "165x605+85+2370" "%~1\Q2.tif"
convert "%~2_1.tif" -crop "165x865+1285+960" "%~1\Q3.tif"
convert "%~2_1.tif" -crop "165x425+1285+2400" "%~1\Q4.tif"
convert "%~2_1.tif" -crop "165x400+1285+2975" "%~1\Q5.tif"

composite -geometry +85+1350 "%~1\Q1.tif" "%~2_1.tif" "%~1\T1.tif"
composite -geometry +85+2370 "%~1\Q2.tif"  "%~1\T1.tif" "%~1\T2.tif"
composite -geometry +1285+960 "%~1\Q3.tif" "%~1\T2.tif" "%~1\T1.tif"
composite -geometry +1285+2400 "%~1\Q4.tif" "%~1\T1.tif" "%~1\T2.tif"
composite -geometry +1285+2975 "%~1\Q5.tif" "%~1\T2.tif" "%~1\T1.tif"
convert "%~1\T1.tif" -compress group4 -define quantum:polarity=min-is-white "%~2_1.tif"
You have no need of mpr:


You are basically extracting area from one document then overlaying them into... THE SAME DOCUMENT, at the SAME LOCATION?

I'll assum it isn't really the same document, but a different 'source' and destination document, with posibly different locations. I'll also assume that you really do want a copy of the cropped sections in the 'Q' images. If you don't need those images you can just remove the extra -writes.

NOTE. I will try to write this in DOS script style, but as i am a UNIX user I can't test it directly.

WARNING: -write does NOT delete the image from memory, it only writes the
current image sequence (usally one image) to a file.

What we do is read in both documents, then bit by bit extract one area, and paste it into the other document, replaceing that document for the updated version between each set of parenthesis.

Code: Select all

convert   input.tif   destination.tif  ^
   ( -clone 0 -crop "165x375+85+1350" +repage  -write "Q1.tif"   ^
     -clone 1 +swap -geometry "+85+1350" -composite ^
   ) +swap +delete ^
   ( -clone 0 -crop "165x605+85+2370" +repage  -write "Q2.tif"   ^
     -clone 1 +swap -geometry "+85+2370" -composite ^
   ) +swap +delete ^
   ( -clone 0 -crop "165x865+1285+960" +repage  -write "Q3.tif"   ^
     -clone 1 +swap -geometry "+1285+960" -composite ^
   ) +swap +delete ^
   ( -clone 0 -crop "165x425+1285+2400" +repage  -write "Q4.tif"   ^
     -clone 1 +swap -geometry "+1285+2400" -composite ^
   ) +swap +delete ^
   ( -clone 0 -crop "165x400+1285+2975" +repage  -write "Q5.tif"   ^
     -clone 1 +swap -geometry "+1285+2975" -composite ^
   ) +swap +delete ^
   -delete 0    -compress group4 -define quantum:polarity=min-is-white ^
   result.tif
The above will reduce the IO time enormously, and keep memory usage to a minimum.

For more info see IM Examples, Basics, Parenthesis
http://www.imagemagick.org/Usage/basics/#parenthesis
as well as the other image sequence modifiers in that same area.

Also see Windows DOS Batch scripts
http://www.imagemagick.org/Usage/windows/
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: Multple crops, multiple composites

Post by jaffamuffin »

wow. nice response.
You are basically extracting area from one document then overlaying them into... THE SAME DOCUMENT, at the SAME LOCATION?
Well, yes, but in between the cropping and the compositing I was processing the cropped region with an external program to remove some detail from the image.
I'll assum it isn't really the same document, but a different 'source' and destination document, with posibly different locations. I'll also assume that you really do want a copy of the cropped sections in the 'Q' images. If you don't need those images you can just remove the extra -writes.
OK, I was processing the Q files as I said to remove data, but using this method, I perfom the external operation on the a copy of the whole image and use that in place of the Q files, so no q files are needed. They were effectively just temp files.
NOTE. I will try to write this in DOS script style, but as i am a UNIX user I can't test it directly.
worked first time.... about 1.3 secs to perform the operation... from about 3-4 secs.

Code: Select all

convert   input.tif   destination.tif  ^
   ( -clone 0 -crop "165x375+85+1350" +repage  -write "Q1.tif"   ^
     -clone 1 +swap -geometry "+85+1350" -composite ^
   ) +swap +delete ^
   ( -clone 0 -crop "165x605+85+2370" +repage  -write "Q2.tif"   ^
     -clone 1 +swap -geometry "+85+2370" -composite ^
   ) +swap +delete ^
   ( -clone 0 -crop "165x865+1285+960" +repage  -write "Q3.tif"   ^
     -clone 1 +swap -geometry "+1285+960" -composite ^
   ) +swap +delete ^
   ( -clone 0 -crop "165x425+1285+2400" +repage  -write "Q4.tif"   ^
     -clone 1 +swap -geometry "+1285+2400" -composite ^
   ) +swap +delete ^
   ( -clone 0 -crop "165x400+1285+2975" +repage  -write "Q5.tif"   ^
     -clone 1 +swap -geometry "+1285+2975" -composite ^
   ) +swap +delete ^
   -delete 0    -compress group4 -define quantum:polarity=min-is-white ^
   result.tif
The above will reduce the IO time enormously, and keep memory usage to a minimum.

For more info see IM Examples, Basics, Parenthesis
http://www.imagemagick.org/Usage/basics/#parenthesis
as well as the other image sequence modifiers in that same area.

Also see Windows DOS Batch scripts
http://www.imagemagick.org/Usage/windows/
[/quote]
will check these out. I actually got a one liner working with -page and -flatten, but this is much quicker, still.

In addition:

If you know how is it possible to get from this:
Image to this: Image or Image
the 3rd one is the same question with a different box ticked, processed differently.

using imagemagick, ideally a command that could be inserted into the above fragment. The sources are binary G4 tiffs, and I am currently using EMC qui ck scan ran from the command line to process the mwith a few dilations, (to fill the box with the cross), then some erosions, which removes the empty boxes but leaves a mark for the filled boxes. Then a small bix of noise removeal to remove extra loose pixels.

This is a preprocess for Optical mark recognition, as these are scans of a questionaire I am trying to process. I don't think imagemagick does these commands natively?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Multple crops, multiple composites

Post by anthony »

Dialation can be done for black and white images by using either
a blur and threshold, or a -convolve and and a threshold..


Fred Wienhaus's script 'morphology' does this and is a good example.
You can pull the operations use from his script.

http://www.fmwconcepts.com/imagemagick/ ... /index.php

Specifically for black and white images...
A dialation is...
convert $TMP -convolve "1,1,1,1,1,1,1,1,1" -threshold 0 $TMP

While an erode is
convert $TMP -convolve "1,1,1,1,1,1,1,1,1" -threshold 99% $TMP

open and close are naturally composites of these operators.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: Multple crops, multiple composites

Post by jaffamuffin »

anthony wrote:Dialation can be done for black and white images by using either
a blur and threshold, or a -convolve and and a threshold..


Fred Wienhaus's script 'morphology' does this and is a good example.
You can pull the operations use from his script.

http://www.fmwconcepts.com/imagemagick/ ... /index.php

Specifically for black and white images...
A dialation is...
convert $TMP -convolve "1,1,1,1,1,1,1,1,1" -threshold 0 $TMP

While an erode is
convert $TMP -convolve "1,1,1,1,1,1,1,1,1" -threshold 99% $TMP

open and close are naturally composites of these operators.

OK. Yes I worked this out and I saw that web page referenced above.

However I still had issues of one sort or another and solving one brought up another.

A problem occurs as to when to do the convolving. If I take the script above with the cloning, it requires that the whole image be copied and convoled first and then what it does is to clone the convoled section into the source image and output a new image.
Convolving a 300dpi A4 scan with a bunch of text (3600,2400 pixels) takes a long time.

So then looking at the above it should be possible just to add the convolve command into the sequence like this:

Code: Select all

( -clone 0 -crop "165x400+1285+2975" +repage   ^
     -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 
     -clone 1 +swap -geometry "+1285+2975" -composite ^
) +swap +delete ^
Which works.

Then, I realise that the whole point of that script was to clone the already convoled part I may as well just do this:

Code: Select all

convert input.tif -region "165x425+1285+2400" ^
    -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 out.tif
I did a script to loop through that and do it for each section, and I think I tested theorectically putting that in something similar to the clone script above, thereby reducing IO time however...

That convolve command does not do all I need. Taht simply expands the pixels by 1 or so.

The convolve script I need is somethign approximating 1 of these 3:

Code: Select all

@echo off
ECHO %TIME%
	convert %1 ^
	-convolve "0,1,0,1,1,1,0,1,0" -threshold 1 ^
	-convolve "0,1,0,1,1,1,0,1,0" -threshold 1 ^
	-convolve "1,0,1,0,1,0,1,0,1" -threshold 254 ^
	-convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	c:\out.tif

	ECHO %TIME%
	convert %1 ^
	-convolve "0,1,0,1,1,1,0,1,0" -threshold 1 ^
	-convolve "0,1,0,1,1,1,0,1,0" -threshold 1 ^
	-median 2 ^
	-convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	c:\out2.tif
	
	ECHO %TIME%
	convert %1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 254 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 -convolve "1,1,1,1,1,1,1,1,1" -threshold 1 ^
	 c:\out1.tif
	 ECHO %TIME%
I also used a larger kernel, right up to around 7-800 1's, (i can't remember) but Although it meant it did it in one pass (to expand) it used more time and more characters.

(Also, -1 seems to do something, but I couldn't work it out. ?? )

But windows command line has a max character limit of 8191 characters, and when I plug these into the above scripts, I go over the limit. Otherwise I need to make more passes on the file which increases IO time.




This form has 5 questions on this side, the second side hs about 9 or so, so ideally the script would need to handle an arbitary number of 'convolve' iterations.

I thought -bench might have done it (even if it spits out a time) but It appears to not to produce any output.

Does IM have some sort of macro - I could define the operation as a macro and just call it throught the command line at various places..?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Multple crops, multiple composites

Post by anthony »

Okay a couple of points...

First do not use -threshold 254 That will suddenly fail if you move to a Q16 version of IM instead of the Q8 version you are obviously using. Use a percentage like 99.999% instead.

Secondly -threshold 0 means that 0 and below is 'black' never mind what the manual says that is what it does. So use -threshold 0 instead of -threshold 1.

Now by dilating multiple times then eroding multiple types you are performing Open and Close operation. However multiple dilations can also be achieved by using a larger kernel

For example 9 '1's given a 3x3 kernel, but 25 '1' will generate a 5x5 kernel and will be equivalent to 2 of the 3x3 kernels, similarly 49 '1' is a 7x7 kernel or equivalent to 3 of the 3x3 kernal results.

HOWEVER there is a little trick you can use for the same results. -gaussian Nx65535 when N is the kernel size. The '65535' is a very very LARGE sigma generating a flat kernel of '1's, while the 'N' is the radius limit defining a kernel size. Weird but true..

As such -convolve "1,1,1,1,1,1,1,1,1" can be expressed as -gaussian 1x65535
and ... doing this 8 times can be expressed using -gaussian 8x65535

So ... your long list of convolves can be expressed faster using...

Code: Select all

convert %1 ^
    -gaussian 8x65535 -threshold 99.999% ^
    -gaussian 14x65535 -threshold 0 ^
    c:\out1.tif
    ECHO %TIME%
A lot simpler to write!!!!! In the library -gaussian actually uses the same routine as -convolve!!!

You may be able to replace -gaussian with -blur, which is a LOT faster due to the way it is implemented. However as -blur is a two pass linear convolution rather than a true 2-dimensional convolution, you will more than likely get more of a 'diamond' shaped (or 'plus') kernel, than a 'square' kernel you are wanting. Experiment and see.

NOTE I have plans to build these morphological functions into IM, but never seem to get the time to do so. :-(

Sorry no macros are in IM, though there are plans for a IM v7 type of 'convert' which will read image processing operations as a file or pipe stream, making it effectively infinite command length. This may expand later into 'proper 'functions' and macros, but that is still more a dream than reality. We are desperately short of people willing to program and contribute to IM core library.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply