distort SRT internal crop example not working for me

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

distort SRT internal crop example not working for me

Post by jaffamuffin »

Hi All
edit - my os and ver sion is first machine

Code: Select all

PsInfo v1.77 - Local and remote system information viewer
Copyright (C) 2001-2009 Mark Russinovich
Sysinternals - www.sysinternals.com

System information for \\HPWS02:
Uptime:                    4 days 22 hours 16 minutes 26 seconds
Kernel version:            Windows 7 Professional, Multiprocessor Free
Product type:              Professional
Product version:           6.1
Service pack:              0
Kernel build number:       7601
Registered organization:   Hewlett-Packard Company
Registered owner:         
IE version:                9.0000
System root:               C:\Windows
Processors:                8
Processor speed:           3.3 GHz
Processor type:            Intel(R) Xeon(R) CPU E3-1245 V2 @
Physical memory:           3988 MB
Video driver:              Intel(R) HD Graphics

c:\pyscripts>convert -version
Version: ImageMagick 6.8.6-8 2013-08-04 Q8 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC OpenMP
Delegates: bzlib djvu fftw fontconfig freetype jng jp2 jpeg lcms lzma openexr pa
ngo png ps tiff webp x xml zlib

2nd machine, same results

Code: Select all

PsInfo v1.77 - Local and remote system information viewer
Copyright (C) 2001-2009 Mark Russinovich
Sysinternals - www.sysinternals.com

System information for \\PROC01:
Uptime:                    2 days 17 hours 47 minutes 42 seconds
Kernel version:            Windows 7 Professional, Multiprocessor Free
Product type:              Professional
Product version:           6.1
Service pack:              0
Kernel build number:       7601
Registered organization:
Registered owner:          Windows User
IE version:                9.0000
System root:               C:\Windows
Processors:                16
Processor speed:           2.3 GHz
Processor type:            Intel(R) Xeon(R) CPU           E5620  @
Physical memory:           0 MB
Video driver:              NVIDIA Quadro NVS 295

b:\pyscripts>convert -version
Version: ImageMagick 6.8.9-10 Q8 x64 2014-11-02 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo freetype jbig jng jp2 jpeg lcms lqr pangocairo png ps rsv
g tiff webp xml zlib

also ran on a linux box here debian, with the following in case of a window/linux translation prob -- same result

Code: Select all

Version: ImageMagick 6.6.0-4 2012-05-02 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2010 ImageMagick Studio LLC
Features: OpenMP
I am processing scanned images to deskew and wish to have a 'clean' edge to the resulting image (avoiding 'black triangles / image distortion etc')

I read this thread http://www.imagemagick.org/Usage/forum_ ... 62&p=73079 and this info page http://www.imagemagick.org/Usage/distorts/ and it looks like something I would be looking for, that is - and internal crop of the scanned image after it has been rotated, such that it would contain the largest shape with same aspect ratio.

I have attached LQ jpgs images for vewing and my script (on windows) .

source image (it has 0.783292 degrees of rotation) 0003x.jpg
Image

Image when ran through deskew 60% ( i find 60 works better than 40 most times) (as an aside I need the deskew to be 'center weighted' -- is that possible ?, it seems the deskew weights it's decision to the top left ??(note not this image) maybe a topic for another discussion) - note the white triangles - 0003x_deskew_60.jpg
Image

Image ran through simple SRT rotate - this extends the image edges which is sometime Ok, but generally not desirable. + or - still exhibits the same issue to a different degree. (shaving is a crude method to resolve this but the amount of shave required changes image by image) 0003x_plus_distort.jpg / 0003x_minus_distort.jpg
Image

Here is the script that I used, and below is the output image. It significantly crops in the page. 0003x_SRT_rotate_output.png
Image

Code: Select all

@echo off
SETLOCAL
  REM angle=20
  REM ratio=`convert rose: -format \
     REM "%[fx:aa=$angle*pi/180; min(w,h)/(w*abs(sin(aa))+h*abs(cos(aa)))]" \
     REM info:`
  REM crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]"
  REM crop="$crop+%[fx:ceil((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
  REM convert rose: -set option:distort:viewport "$crop" \
  REM +distort SRT $angle +repage   rotate_internal.png

  REM convert rose: -distort SRT "%[fx:aa=20*pi/180;(w*abs(sin(aa))+h*abs(cos(aa)))/min(w,h)], 20"    rotate_correction.png

FOR /F %%B IN ('convert %1  -deskew 60%% -format %%[deskew:angle] info:') DO (
    SET angle=%%B
    ECHO angle %%B
)
REM SET angle=45

FOR /F "usebackq" %%B IN (`convert %1 -format "%%[fx:aa=%angle%*pi/180; min(w,h)/(w*abs(sin(aa))+h*abs(cos(aa)))]" info:`) DO (
    SET ratio=%%B
    ECHO ratio %%B
)
REM SET ratio=0.5

SET crop1=%%[fx:floor(w*%ratio%)]x%%[fx:floor(h*%ratio%)]
SET crop=%crop1%+%%[fx:ceil((w-w*%ratio%)/2)]+%%[fx:ceil((h-h*%ratio%)/2)]
convert %1 -set option:distort:viewport "%crop%" +distort SRT "%angle%"  rotate_internal.png
And here is my desired output image. 0003x_desired.jpg
Image

Any questions let me know -- It seems the the script part fx that calculates the ratio /w/h seems to be significantly reducing the veiwport width for the reason of which i do not know.

http://81.143.7.209/0003x.zip
Last edited by jaffamuffin on 2015-04-16T04:57:12-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: distort SRT internal crop example not working for me

Post by snibgo »

Good work on the script.

Running the script on your input, I get angle = 0.783292 (degrees), and ratio = 0.717003.

If I correctly understand the meaning of ratio, this is very distant from 1.0 for such a small angle.

If the angle is zero, your expression for ratio gives the result 0.7240325865580448, where I would expect 1.0.

Therefore, either my understanding is wrong, or your expression for ratio is wrong.
snibgo's IM pages: im.snibgo.com
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: distort SRT internal crop example not working for me

Post by jaffamuffin »

Yes, I got that angle too, i mistyped in my post, now edited. I copied pasted the script from the info page above into my linux bash terminal and substituted the names of my file as below. Same result on my linux box, so ... perhaps the code on the page is wrong ?

Code: Select all

#!/bin/bash
angle=0.783292
ratio=`convert 0003x.jpg -format \
     "%[fx:aa=$angle*pi/180; min(w,h)/(w*abs(sin(aa))+h*abs(cos(aa)))]" \
     info:`
crop="%[fx:floor(w*$ratio)]x%[fx:floor(h*$ratio)]"
crop="$crop+%[fx:ceil((w-w*$ratio)/2)]+%[fx:ceil((h-h*$ratio)/2)]"
convert 0003x.jpg -set option:distort:viewport "$crop" \
          +distort SRT $angle +repage   rotate_internal.png
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: distort SRT internal crop example not working for me

Post by jaffamuffin »

Hmmm - I swapped sin and cos about so from

Code: Select all

convert %1 -format "%%[fx:aa=%angle%*pi/180; min(w,h)/(w*abs(sin(aa))+h*abs(cos(aa)))]" info:
to

Code: Select all

convert %1 -format "%%[fx:aa=%angle%*pi/180; min(w,h)/(w*abs(cos(aa))+h*abs(sin(aa)))]" info:
and now I think it works!

I wonder if this was caused by the script being developed originally for TV (see orig thread) which are landscape and it has inverted as this is a portrait image. But I'm sure I read somewhere that that didn't matter whether you calc the height or the width
Can someone confirm the findings here ? (and maybe update the info page ? )
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: distort SRT internal crop example not working for me

Post by snibgo »

Ah, okay, this has come from Anthony's page http://www.imagemagick.org/Usage/distor ... te_methods and thread post viewtopic.php?t=18862&p=73079#p73079

The maths looks wrong. Anthony says:

Code: Select all

scale = ( w*sin(a) + h*cos(a) ) / h

NOTE as this is a ratio it did not matter that I used w,h and not w/2,h/2
Also I get exactly the same formula if I made the ratio using the y coodinate with height.
The 'h' on the bottom represents the smaller dimension of width and height!

at an angle of zero scale = 1.0
On the first line, the two "h"s represent two different things (eek!). When the angle is zero, the result is 1.0 only when the divisor is the height, which occurs when height < width.

When height > width, the scale at angle zero will be height / width.
snibgo's IM pages: im.snibgo.com
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: distort SRT internal crop example not working for me

Post by jaffamuffin »

OK , so i'm on the right track then. Would be good to clarify this somewhere as I had been puzzling this for a day or 2. But it would be even better if IM could have a built in 'internal crop' command or something. (which i imagine would be a shortcut to this kind of thing anyway, like rotate is)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: distort SRT internal crop example not working for me

Post by snibgo »

This works:

Code: Select all

FOR /F "usebackq" %%B IN (`%IM%convert %1 ^
  -format "%%[fx:aa=%angle%*pi/180; h<w?h/(w*abs(sin(aa))+h*abs(cos(aa))):w/(h*abs(sin(aa))+w*abs(cos(aa)))]" ^
  info:`) DO (
    SET ratio=%%B
    ECHO ratio %%B
)
When angle is zero and h<w, this is h/h = 1.0
When angle is zero and h>=w, this is w/w = 1.0
snibgo's IM pages: im.snibgo.com
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: distort SRT internal crop example not working for me

Post by jaffamuffin »

Works nicely indeed. Thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: distort SRT internal crop example not working for me

Post by fmw42 »

If you are on LInux/MacOSX or Windows with Cygwin, see my script, innercrop, at the link below. I tested it on your deskewed image and it seemed to work fine.
jaffamuffin
Posts: 59
Joined: 2009-01-30T03:46:08-07:00

Re: distort SRT internal crop example not working for me

Post by jaffamuffin »

Hi Fred

I haven't you tried your innercrop script. I will test it later on some more challenging images. I'm intrigued with the sunset picture example - can the sensitivity of the the allowance be tolerated ?

I'm thinking for a scanned page that maybe has a v rip in the page, I would want to include probably most of the V rip, but only to the majority of the page edge. IYSWIM.
Post Reply