Deskew an original image, based on an enhanced clone

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
B_Gaspar
Posts: 16
Joined: 2013-07-25T15:34:49-07:00
Authentication code: 6789

Deskew an original image, based on an enhanced clone

Post by B_Gaspar »

I've been working with Photoshop for years, in creating automated complex processes to deskew and crop scanned images from paper and books. Also making enhanced copies for OCR or publishing. Now I want to make the transition to Imagemagick. Is it possible to clone an original image, apply filters to enhance edges and visibility then use the deskew in the original, but based on the result of filtered clone?
The idea is to keep the original untouched and just deskew and crop.
I'm a real noob, and i've been experimenting, reading posts, manuals and can't seem to get it right.
This is a far as i've got
I'm using the latest version of Imagemagick, Cygwin on Windows XP Sp3

Code: Select all

convert /IMAGEPROCESS/TESTEIN/teste003.tif     -background black \( +clone    -threshold 40%  -write show: -verbose -deskew 40% -write show: \)  +repage /IMAGEPROCESS/TESTEOUT/teste003.tif
B_Gaspar
Posts: 16
Joined: 2013-07-25T15:34:49-07:00
Authentication code: 6789

Re: Deskew an original image, based on an enhanced clone

Post by B_Gaspar »

Something like this might do the trick:
anthony wrote:okay...

I have added a little code (both Imv6 and Imv7) to save the calculated deskew angle as an artifact.
In SVN, revision 11784 Im version 6.8.4-9

For example...

Code: Select all

convert rose: -background black -rotate 5 \
            -deskew 60 \
            -print 'image deskewed by %[deskew:angle]\n' show:
returned... image deskewed by -4.02199

If deskew failed this result will be 0 (or -0!)

However I never liked deskew as I found it rather buggy. Which is also why I have no IM Examples of its usage.
But I can't seem to make it work. If I could get that angle and use it on -rotate
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Deskew an original image, based on an enhanced clone

Post by fmw42 »

I really do not follow what you are trying to do? Which image do you want deskewed the original or the processed?

-rotate does not know about % escapes, but -distort SRT does. So the following will do what you are trying, but it just rotates the deskewed image so that it is rotated back the wrong way.

Code: Select all

convert rose: -background black -rotate 5 -write show: \
-deskew 40 -print 'image deskewed by %[deskew:angle]\n' \
-distort SRT '%[deskew:angle]' show:
I believe you will need to extract the angle and put it into a variable in one command and then use the variable in another command to rotate the image you want. -rotate or -distort SRT can use variables. If quoted, then they must be double quotes.

Code: Select all

angle=`convert rose: -background black -rotate 5 -deskew 40 -format '%[deskew:angle]' info:`
echo $angle
-4.02199
B_Gaspar
Posts: 16
Joined: 2013-07-25T15:34:49-07:00
Authentication code: 6789

Re: Deskew an original image, based on an enhanced clone

Post by B_Gaspar »

We work with scanned books with this output which we will consider the original.
This image comes from the scanner this way. And we need to work in high resolutions so this image will be at 300 dpi with Width 33,4 inches and Height 21 inches.
Image
Now I need to straighten or deskew, trim, split the two pages, deskew and trim again.
So I use a bunch of filters to turn the image as easier as possible to compute not only the deskew but the trim also. But i need to do this on the side and apply the result to the original.
If I apply deskew to this image I get e pretty good result.
Image
Also, since we are working with big images, if I would make a clone or copy, downsize it, take the color off, I could fasten the whole operation. (last project was with 20 000 images like this one). apply the filters deskew, get the angle, use it on the original. Later on get the best trim possible, apply the results on the original. (the trim should not cut any part of the original)
Final result would be this.
Image

So using a copy of the original, apply the results to the original. If I can understand this, I can create and evolve my programming.
All of this I already do on photoshop through batch.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Deskew an original image, based on an enhanced clone

Post by fmw42 »

If you are on Linux/Mac (or Windows w/Cygwin), you might try my script, textcleaner, at the link below. It has a built in deskew (-u) option.

Note that -deskew does not work well above about 5degree rotation.

P.S. I also have an unrotate script that works better for large angles, but may not work well on images of text. But you can try it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Deskew an original image, based on an enhanced clone

Post by fmw42 »

All of what you want to do can been done in a shell script or possible a DOS Bat script. I do not think it can be done in one command.

You need to process the image, then deskew and get the deskew angle and put it into a variable. Then process the original image with that angle to rotate it using the deskew variable. Then do whatever else you need to do.

All the steps can be put into a script that can be set up to loop over a directory and do that to all the images in the directory.
B_Gaspar
Posts: 16
Joined: 2013-07-25T15:34:49-07:00
Authentication code: 6789

Re: Deskew an original image, based on an enhanced clone

Post by B_Gaspar »

Thank you so much for the Help. I am close to zero at programming, so its not easy to understand the logic. I tried your idea and it gave a nem direction. Your scripts are great and a source of inspiration. Unrotate gives strange results and out of memory probs. Our work is roughly 70% books and newspapers, bounded, 10% large format, (blueprints, maps), 8% sheets of paper and rest miscenaleous which includes scans from microfilm, photography, slidez etc.
I will work on it and post the results.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Deskew an original image, based on an enhanced clone

Post by fmw42 »

Post a link to a full size image similar to the one above that is right from the scanner? Let me show you what results I get from textcleaner. Then you can test your other steps. The image above is too low a resolution (WxH) to produce anything meaningful.

What image format does your scanner output? JPG, TIFF, PDF
B_Gaspar
Posts: 16
Joined: 2013-07-25T15:34:49-07:00
Authentication code: 6789

Re: Deskew an original image, based on an enhanced clone

Post by B_Gaspar »

Our scanners output to any format, but we always use TIF uncompressed. This will be the master. All further processing will be done on duplicates.
Ok here goes. One of the biggest newspapers we have worked with. The file is in RAR 50% less space. https://www.dropbox.com/s/arqzojlfqtm4s ... xample.rar.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Deskew an original image, based on an enhanced clone

Post by fmw42 »

Here are 3 results from my textcleaner

filter (-f 50) large enough for most but not the largest size text, no deskew (-u)
textcleaner -g -e normalize -f 50 -o 10 0.tiff 0_g_norm_f50_o10.tiff
http://www.fmwconcepts.com/misc_tests/t ... 0_o10.tiff


filter (-f 50) large enough for most but not the largest size text, with deskew (-u)
textcleaner -g -e normalize -f 50 -o 10 0.tiff 0_g_norm_f50_o10.tiff
http://www.fmwconcepts.com/misc_tests/t ... o10_u.tiff


filter (-f 250) large enough for the largest size text, no deskew (-u)
textcleaner -g -e normalize -f 250 -o 10 0.tiff 0_g_norm_f250_o10.tiff
http://www.fmwconcepts.com/misc_tests/t ... 0_o10.tiff

For this pass, you probably do not want deskew, because both halves have a different skew. So once you split in half and crop, you can deskew each side separately.
B_Gaspar
Posts: 16
Joined: 2013-07-25T15:34:49-07:00
Authentication code: 6789

Re: Deskew an original image, based on an enhanced clone

Post by B_Gaspar »

Again thank you for all the help. I have been unable to complete my project due to too much work going on right now in my company. I got the code working like I want it, now I want more complex treatment. I will share my results as soon as I have them. Again, thanks for all the help
Post Reply