Page 1 of 1

deskew

Posted: 2019-10-18T07:51:44-07:00
by jaffamuffin
Hi all
I'm trying to deskew scanned images but weird things are happening. https://gofile.io/?c=ZgXVTY

here is my command

Code: Select all

FOR /F %%B IN ('convert %1 -gravity center -crop 1000x1000+0+0 -deskew 40%% -format %%[deskew:angle] info:') DO (
    SET angle=%%B
    echo angle %%B
)
convert %1 -rotate "%angle%" +repage %2
The main thing is the first command convert %1 -gravity center -crop 1000x1000+0+0 -deskew 40%% -format %%[deskew:angle] info: to detect the angle required.

Now I know my test image is skewed by 1.5 degrees. it's a yellow A4 page on black background. 300 dpi :
300_01.tif TIFF 2742x3756 2742x3756+0+0 8-bit sRGB 29.4659MiB 0.000u 0:00.000

1. If I run this i get a value of 0.111 degrees
2. If I crop the image to 1000x1000 and then run it on the newly cropped image I get 1.45 degree, correct.
3. If I remove the crop part of the command and run it on the original image i get 0.27 degrees.
4. if i remove the crop part of the command and run it on the crop i get also 1.45

So I can't inline crop ? , but when I do I get a different result to supplying an already cropped image. I don't want expensive temp files and read.write ops ? I thought I could do it this way with imagemagick ?

Also have tried a +repage after the crop but makes no difference.

Re: deskew

Posted: 2019-10-18T08:28:16-07:00
by snibgo
I don't understand your points 3 and 4. Showing the actual commands would help.

Pre-processing before "-deskew" usually helps, eg:

Code: Select all

magick 300_01.tif ( +clone -fuzz 10% -fill White +opaque Black -deskew 40% -set option:DESK %[deskew:angle] +delete ) -rotate %[DESK] out.png
This needs IM v7. It processes a copy to find the angle, then rotates the original by that angle.

Re: deskew

Posted: 2019-10-18T09:07:36-07:00
by jaffamuffin
i'm using version
Version: ImageMagick 7.0.7-22 Q8 x64 2018-01-22 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo flif freetype gslib jng jp2 jpeg lcms lqr open
exr pangocairo png ps raw rsvg tiff webp xml zlib


as for each command :
1. convert ORIG.TIF -gravity center -crop 1000x1000+0+0 -deskew 40%% -format %%[deskew:angle] info: gives 0.11 degrees
2. if i use crop it i.e. convert %1 -gravity center -crop 1000x1000+0+0 1000pxcrop.tif. Then Feed 1000pxcrop.tif into my command above, I get 1.45 degrees, OK
3. if i do this : convert ORIG.TIF -deskew 40%% -format %%[deskew:angle] info: I get 0.27 degrees
4. convert "1000pxcrop.tif" -deskew 40%% -format %%[deskew:angle] info: same 1.45 degrees

Re: deskew

Posted: 2019-10-18T10:04:12-07:00
by jaffamuffin
another way to look at it :
file as in first post is 300_01.tif


This code supposedly takes a 1000x1000 section from the top middle and bottom of the supplied image and prints the detected skew angle. (test.bat)

Code: Select all

FOR /F %%B IN ('convert %1 -contrast-stretch 10%%x10%% -gravity center -crop 1000x1000+0+0 +repage  +deskew  -format %%[deskew:angle] info:') DO (
    SET angle=%%B
    echo angle %%B
)
FOR /F %%B IN ('convert %1 -contrast-stretch 10%%x10%% -gravity north -crop 1000x1000+0+0 +repage  +deskew  -format %%[deskew:angle] info:') DO (
    SET angle=%%B
    echo angle %%B
)
FOR /F %%B IN ('convert %1 -contrast-stretch 10%%x10%%  -gravity south -crop 1000x1000+0+0 +repage   +deskew -format %%[deskew:angle] info:') DO (
    SET angle=%%B
    echo angle %%B
)
If I prepare the same 3 sections top middle bottom independently :

D:\samples>convert 300_01.tif -gravity south -crop 1000x1000+0+0 +repage south.tif

D:\samples>convert 300_01.tif -gravity center -crop 1000x1000+0+0 +repage center.tif

D:\samples>convert 300_01.tif -gravity north -crop 1000x1000+0+0 +repage north.tif

north south and center should print the same 3 values as there is only one possible 1000x1000 area to select:

D:\samples>test.bat center.tif
angle 0.1119056770662069
angle 0.1119056770662069
angle 0.1119056770662069

D:\samples>test.bat north.tif
angle -0.1119056770662069
angle -0.1119056770662069
angle -0.1119056770662069

D:\samples>test.bat south.tif
angle 1.2307746699577373
angle 1.2307746699577373
angle 1.2307746699577373


BUT I Would expect this file (the original image) to print same as the values above i,e, expected output should be
angle -0.1119056770662069
angle 1.2307746699577373
angle 0.1119056770662069

BUT I get this instead. Actually, 1.45 is the correct and best value.
D:\samples>test.bat 300_01.tif
angle 1.4544631491195343 (center)
angle 1.3426240265377571 (north)
angle 1.2307746699577373 (south)

What am i doing wrong ?

Re: deskew

Posted: 2019-10-18T12:45:01-07:00
by snibgo
jaffamuffin wrote:What am i doing wrong ?
Nothing wrong, as such. You are comparing the results from (a) contrast-stretch and then crop with (b) crop and then contrast-stretch. The images in these two cases are different, so the deskew angles are also different.

Re: deskew

Posted: 2019-10-18T14:29:58-07:00
by fmw42
contrast-stretch looks at the histogram. When you crop, the histogram will differ from the original.

Re: deskew

Posted: 2019-10-19T12:47:14-07:00
by jaffamuffin
got it. good point thanks.