Page 1 of 1

Find and replace white rectangle

Posted: 2012-01-31T08:08:02-07:00
by dawson
I need to search a white and rectangle area into a PDF file to replace it with an image, example:

this: http://dawsonbrito.info/im/Documento1.pdf

into this http://dawsonbrito.info/im/Documento2.pdf

The problem is that the white area is not always at the same place, do you have any ideia if it's possible do that with im?

Re: Find and replace white rectangle

Posted: 2012-01-31T10:56:04-07:00
by fmw42
First IM will convert your PDF from vector to raster and then back to vector. See http://www.imagemagick.org/Usage/formats/#vector

You can use compare to search a larger image to find the location of a smaller image (such a white rectangle), but it will give either the first or last position that is suitable, I am not sure which, but I think it is the first. So in your image it would find the upper left corner.

see
http://www.imagemagick.org/script/compare.php
viewtopic.php?f=1&t=14613&p=51076&hilit ... ric#p51076
(note in the latter, on current versions of IM, you need to also add -subimage-search)

See my script, autocaption and autolabel, for examples of doing something similar with text.

Re: Find and replace white rectangle

Posted: 2012-01-31T16:56:41-07:00
by anthony
To find the largest white area convert the image to white on black mask, black being anything not white. Set a virtual pixel of black too, then use distance morphology to locate the largest 'distance; from any black point, using 'Chebyshev' distances.
http://www.imagemagick.org/Usage/morphology/#chebyshev

One the distance has been done 'auto-level' the image and any set of white pixels will show the largest white area in the image.

Note morphology distance after version v6.6.9-4 is fast as it only requires two passes through the image (once top to bottom and again bottom to top) to do its task. Before that version multiple (top down only) passes was needed.

Re: Find and replace white rectangle

Posted: 2012-02-29T10:28:29-07:00
by dawson
I couldn't make it work like this, but now I only need to put in one of the corners and that I can make. Thanks for helpping.

But now I'm with one other problem, when I call the PHP that call the shell script from a browser it doesn't work it shows this message:

Code: Select all

convert: Missing Null Image List Separator layers Composite @ mogrify.c/MogrifyImageList/7899.
The commanda that i run is this:

Code: Select all

convert -density 300 -quality 100 -compress fax -monochrome -alpha off "/opt/lampp/htdocs/pdf/upload/$2" null: /opt/lampp/htdocs/pdf/carimboover.png -gravity $3 -compose multiply -layers composite "/opt/lampp/htdocs/pdf/autenticados/$2" 2>&1
the php code:

Code: Select all

exec("sh /opt/lampp/htdocs/pdf/assinador.sh 2012127444857741000-001 'Flash2.pdf' SouthWest");
If I run this from console It works great, even if I call the PHP file from the console it also works fine, other thing is that if instead of a PDF I send an JPG or PNG file it works fine too.

Re: Find and replace white rectangle

Posted: 2012-02-29T18:59:53-07:00
by anthony
Please start a new topic for your new problem!!!!!

As for you not getting the chebyshev distance solution, how about providing a small test image? A smaller resized image of a PDF page would be fine for testing purposes. With an example we can try it out and actually show you how to do it.

Re: Find and replace white rectangle

Posted: 2012-03-01T05:03:46-07:00
by dawson
PDF sample: http://dawsonbrito.info/im/output.pdf

The same PDF just converted to PNG: http://dawsonbrito.info/im/output.png

The stamp that I wanna put: http://dawsonbrito.info/im/carimboover.png

Re: Find and replace white rectangle

Posted: 2012-03-01T20:56:45-07:00
by anthony
Hmm a very high aspect ratio image. 4:1

That may require a custom distance metric with ratio that is rotated 90 degrees (top and bottom is more costly)

Code: Select all

  convert output.png -virtual-pixel black  -morphology Distance '3: 5,4,5  1,0,1  5,4,5' -auto-level  results.png
The brightest point is the center of the largest 4:1 ratio rectangular area found.
and shows it to be the top corners or somewhere along the bottom edge.

However there is a point on the right side 3/4 of the way done the page (afetr the paragraph starting "Brasilia -DF ..."). It isn't as large though it would be if you didn't want a 'edge' result.
if the page was 'trimmed' so the addition was limited to within the text bounds, somewhere around that location actually does become the 'best' location.


To extract the maximum point(s) and use the 'normalized' image as a mask as a mask to get position and size from the distance result.

Code: Select all

convert output.png -virtual-pixel black  -morphology Distance '3: 5,4,5  1,0,1  5,4,5' \
         \( +clone -auto-level -fill black +opaque white \) \
         -compose multiply -composite -depth 16 txt: |\
  grep -v black
394,53: ( 216, 216, 216) #00D800D800D8 rgb(0.329595%,0.329595%,0.329595%)
395,53: ( 216, 216, 216) #00D800D800D8 rgb(0.329595%,0.329595%,0.329595%)
396,53: ( 216, 216, 216) #00D800D800D8 rgb(0.329595%,0.329595%,0.329595%)
That is a box 216x2+1 by 216x2/4+1 pixels => 433 x 109 pixels in size centered on any of those 3 pixels
(bottom of page).

Re: Find and replace white rectangle

Posted: 2012-05-12T22:06:53-07:00
by waigani
Anthony, I have a similar problem and am trying to follow your instructions. I need the x,y pixel position and width/height of the largest rectangle of any ratio from a chebyshev(ed) image. Full details of my problem are here: http://stackoverflow.com/questions/1056 ... -chebyshev


I don't full understand the output: 396,53: ( 216, 216, 216) #00D800D800D8 rgb(0.329595%,0.329595%,0.329595%) nor the command you used to get it. Is there breakdown of what is going on somewhere on the net that I can read?

Thanks

Re: Find and replace white rectangle

Posted: 2012-05-12T23:00:48-07:00
by fmw42
I believe that 216 is the "half" size of the max dimension of the rectangle (216x2+1). The other dimension is 216x2/4+1. I think then that the center is at 396,53. Though I will defer to Anthony. But in the mean time, you can check this out by measurements in your image. Overlay a rectangle (or draw a rectangle) of these dimensions centered at those coordinates and see if that matches.

Re: Find and replace white rectangle

Posted: 2012-05-13T18:03:07-07:00
by anthony
Fred is correct.

The color value is rectangle size (half distance) according to the 'scale factor' (fixed point integer arithmetic) of the user designed kernel. The 'peak' location is the center pixel of the rectangle. Though you can get a 'line' of peak values, along which the rectangle of that aspect ratio will fit.

NOTE the rectangle is always a odd size with an actual pixel in the very center. If you get two or four peaks of the same value, then the rectangle can be an even number (center is a pixel edge), but Morphology works with pixels, so locations are pixel centric.