Page 1 of 1

Equation for deskew?

Posted: 2016-03-01T03:19:41-07:00
by miguellint
Hello...

I have a line starting at an (x,y) coordinate and finishing at another (x,y) coordinate.

I want the line to be horizontal so I use the -deskew option.

The (x,y) coordinates at the start and end of the now horizontal line are slightly different to the original (x,y) coordinates.

My question is...

Deskew can output the angle of rotation it used to make the line horizontal. Is there an equation into which I can plug that angle and the original start and end coordinates and get back the start and end coordinates for the now horizontal line.

For clarity... below are a few examples giving the original start/end coordinates, the deskew angle and the resulting start/end coordinates for the now horizontal line. In real life, I have several hundred original coordinates and several hundred angles of rotation but no resulting horizontal line coordinates.

I would like to know if there is a quick mathematical way to find the horizontal line start/end coordinates as opposed to IM needing to run for many hours to get the same result.

Using Ubuntu 15.10 and IM 6.8.8-9

Thanks
Miguel

----

Line01.png
Original Line Coordinates - (145,154) (1688,109)
Deskew - angle of rotation: 1.65015
Horizontal Line Coordinates - (150,157) (1694,157)


Line02.png
Original Line Coordinates - (451,118) (1997,84)
Deskew - angle of rotation: 1.21679
Horizontal Line Coordinates - (455,127) (2003,127)


Line03.png
Original Line Coordinates - (87,148) (1634,93)
Deskew - angle of rotation: 3.02745
Horizontal Line Coordinates - (93,152) (1642,152)


Line04.png
Original Line Coordinates - (439,100) (1994,84)
Deskew - angle of rotation: 0.615458
Horizontal Line Coordinates - (443,106) (1997,106)

Re: Equation for deskew?

Posted: 2016-03-01T03:43:46-07:00
by snibgo
I think your question boils down to: given an image dimension WxH, rotated by angle theta about the centre, what are the new coordinates (x',y') for a point that was at (x,y)?

First, subtract half the width and height to get the position relative to the centre. Call this (X,Y). If X increases to the right, and Y increases down, and theta is positive clockwise:

X' = cos(theta).X - sin(theta).Y
Y' = sin(theta).X + cos(theta).Y

(I think that's right, but needs checking.)

Then you need to add the new semi-width and semi-height to X' and Y'.

Re: Equation for deskew?

Posted: 2016-03-01T21:11:43-07:00
by miguellint
What a superb answer.

Thanks very much :-)

Now I just need to sort out a script.

Thanks again
Miguel

Re: Equation for deskew?

Posted: 2016-03-02T22:52:13-07:00
by anthony
While you have a better answer (because you have actual coordinates to do math with), you can also get the deskew angle using artifacts.
deskew like many operators saves the angle it discovered in the image artifacts, which may not be saved in actual image files so you need to get it while image is still in memory.

Example...

Code: Select all

  convert rose: -rotate 5 +deskew show:
Shows IM nicely deskewed a 5 degree rotation

While you can get the angle from verbose identify (or "info:")

Code: Select all

convert rose: -rotate 5 +deskew -verbose info:
...
Artifacts:
deskew:angle: -4.91179
...
Or more specifically

Code: Select all

convert rose: -rotate 5 +deskew -print '%[deskew:angle]\n' null:
-4.91179
Note quite exactly 5 degress but close.

For more info on Settings and Artifacts see..
IM Examples.. Basic Usage..
Image Meta-data: Attributes, Properties and Artifacts
http://www.imagemagick.org/Usage/basics/#settings

Also see Percent Escape Handling
http://www.imagemagick.org/script/escape.php