Deskewing and Cropping Scans using ImageMagick

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
Cicero
Posts: 2
Joined: 2019-08-28T21:51:09-07:00
Authentication code: 1152

Deskewing and Cropping Scans using ImageMagick

Post by Cicero »

Version: ImageMagick 7.0.8-62 Q16 x64
Platform: W10



Attempting to deskew and crop scans of a CD booklet using ImageMagick.

Here's one image which I've been using as an example on which to build my one-liner - https://mega.nz/#!jIQD3aqC!PbjG4_Xaq-YP ... IUkaoQzOgM

So far I've been using something along the lines of;

Code: Select all

convert testscan.png -background white -deskew 80% -fuzz 40% -trim +repage edited_scan.png
The deskewing step works very well, however I've been unable to achieve good results on the cropping stage. When deskewing the image ImageMagick expands it and adds additional pixels to fill in the blanks (here white, but could be transparent as well). This may be interfering with the ability of -trim to crop the image.

In any case, -trim either;
- fails to crop any edges
- crops a single edge but not the others
- (upon raising -fuzz values) crops multiple sides but fails to accurately crop edges

Not exactly sure what the issue is, but I've tried a number of different alterations to -trim parameters without success. If anybody has any advice it would be appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Deskewing and Cropping Scans using ImageMagick

Post by fmw42 »

The problem with your trim is that your light gray background has dark spots in it. It is not a nearly solid color. Look carefully at your image at full resolution. Also you have used white for your deskew background, which is not close enough to your light gray color. So change your background color for the deskew to one closer to your light gray background.

The way to get around this is to create a mask of the main area by thresholding or floodfilling, clean the mask using either morphology close or with connected components. The get the crop dimensions from a trim of the mask and crop the input.


The following does it all in one IM 7 command using connected components to remove small spots from the mask.

Input:
Image

Unix syntax on my Mac

Code: Select all

magick testscan.png -rotate 90 -set option:color "%[pixel:u.p{10,10}]" -background "%[color]" -deskew 80% \
\( -clone 0 -fuzz 20% -fill black -draw "color 10,10 floodfill" -alpha off -fill white +opaque black -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=1000 \
-connected-components 8 -set option:cropbox "%@" +delete \) \
-crop "%[cropbox]" +repage \
edited_scan.png
Image

For Windows syntax, replace end of line \ with ^ and remove the \ from before the opening and closing parenthesis

The 90 degree rotation at the beginning is optional. But eventually you will need to do that.

See
-set option at https://imagemagick.org/Usage/basics/#define
-connected-components at https://imagemagick.org/script/connected-components.php
Cicero
Posts: 2
Joined: 2019-08-28T21:51:09-07:00
Authentication code: 1152

Re: Deskewing and Cropping Scans using ImageMagick

Post by Cicero »

Thanks for the assistance. In the example scan you provided it appears that a part of the booklet is still cut off at the left margin compared to the original scan. Any suggestions for further experimentation?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Deskewing and Cropping Scans using ImageMagick

Post by fmw42 »

The command needs to remove the virtual canvas after the deskew with +repage. I also put in a +fuzz to turn off fuzz for the connected components. Try this:

Code: Select all

magick testscan.png -rotate 90 -set option:color "%[pixel:u.p{10,10}]" -background "%[color]" -deskew 80% +repage \
\( -clone 0 -fuzz 20% -fill black -draw "color 10,10 floodfill" -alpha off +fuzz -fill white +opaque black -type bilevel \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=1000 \
-connected-components 8 -set option:cropbox "%@" +delete \) \
-crop "%[cropbox]" +repage \
edited_scan2.png
Image
Post Reply