ebook distortion

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?".
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

ebook distortion

Post by jumpjack »

I want to perform such a transformation on an image:
Image

I tried with convert -distort Perspective but it does not work.

Which option should I sue to the define the "source polygon" and the "destination polygon" for this transformation?

Alternativeli I could divide image in two images and process them separately... but how to divide one image into 2 ones?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ebook distortion

Post by anthony »

Are the edges straight or curved?

if straight, you will need to divide the image into two separate pages, correct the distion on each page, then rejoin.

If curved you will need some other distortion correction method.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: ebook distortion

Post by jumpjack »

They're "quite" straight (I can consider them as straight, at least for now)
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: ebook distortion

Post by jumpjack »

Ok, I got it:

Code: Select all

rem This script transform the "v-scanning" of a paper book into a rectangular image,
rem by compensating perspective diastortion.

@echo off

set SOURCEIMG="c:\temp\libro-new.JPG"

rem Each distorted page is defined by TopLeft X,Y , TopRight X,Y, BottomRight X,Y and BottomLeft X,Y  coordinates;

rem Top Left X and Bottom Left X are the same; rem Top Right X and Bottom Right X are the same:
set TLX1=527
set TLY1=74
set TRX1=1277
set TRY1=164
set BLX1=%TLX1%
set BLY1=1637
set BRX1=%TRX1%
set BRY1=1410

rem Right coordinates of left image match to left coordinates of right image:
set TLX2=%TRX1%
set TLY2=%TRY1%
set TRX2=1989
set TRY2=31
set BLX2=%TLX2%
set BLY2=1637
set BRX2=%TRX2%
set BRY2=1664


Rem Calculate page width and height:
set /A WIDTH1= %TRX1%-%TLX1%
set /A HEIGHT1= %BLY1%-%TLY1%

set /A WIDTH2= %TRX2%-%TLX2%
set /A HEIGHT2= %BLY2%-%TLY2%

rem Calculate top and bottom offset for right margin of left page:
set /A TopOffset = %TRY1%-%TLY1%
set /a BottomOffset = %TRY1%-%TLY1%+%BRY1%-%TRY1%

rem Debug printing:
echo %WIDTH1%x%HEIGHT1%+%TLX1%+%TLY1% c:\temp\left.jpg
echo %WIDTH2%x%HEIGHT2%+%TLX2%+%TLY1% c:\temp\left.jpg
echo 0,0,1,1   %WIDTH1%,%TopOffset%,%WIDTH1%,0  0,%HEIGHT1%,0,%HEIGHT1%    %WIDTH1%,%BottomOffset%,%WIDTH1%,%HEIGHT1%



rem ******* Left page ********

rem Crop left image:
echo ---------- C:\temp\ImageMagick-6.6.6-Q16-windows\ImageMagick-6.6.6-4\convert %SOURCE_IMG% -crop %WIDTH1%x%HEIGHT1%+%TLX1%+%TLY1% c:\temp\left.jpg
C:\temp\ImageMagick-6.6.6-Q16-windows\ImageMagick-6.6.6-4\convert %SOURCEIMG% -crop %WIDTH1%x%HEIGHT1%+%TLX1%+%TLY1% c:\temp\left.jpg

Rem Adjust left image (fix perspective distortion):
C:\temp\ImageMagick-6.6.6-Q16-windows\ImageMagick-6.6.6-4\convert c:\temp\left.jpg -virtual-pixel white -distort Perspective "0,0,1,1   %WIDTH1%,%TopOffset%,%WIDTH1%,0  0,%HEIGHT1%,0,%HEIGHT1%    %WIDTH1%,%BottomOffset%,%WIDTH1%,%HEIGHT1%" c:\temp\left-ok.jpg


rem ******** Right page *******

rem Crop right image:
C:\temp\ImageMagick-6.6.6-Q16-windows\ImageMagick-6.6.6-4\convert %SOURCEIMG% -crop %WIDTH2%x%HEIGHT2%+%TLX2%+%TLY1% right.jpg

Rem Adjust right image (fix perspective distortion):
C:\temp\ImageMagick-6.6.6-Q16-windows\ImageMagick-6.6.6-4\convert right.jpg -virtual-pixel white -distort Perspective "0,%TopOffset%,0,0   %WIDTH2%,0,%WIDTH2%,0  %WIDTH2%,%HEIGHT2%,%WIDTH2%,%HEIGHT2%   0,%BottomOffset%,0,%HEIGHT2%" c:\temp\right-ok.jpg

Ready to scan your whole bookshelf? :D
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: ebook distortion

Post by jumpjack »

My new source:

Code: Select all

    rem This script transform the "v-scanning" of a paper book into a rectangular image,
    rem by compensating perspective diastortion.

    @echo off
    
    
rem ***********************************************
rem ************ CUSTOM SETTINGS ******************


    set SOURCEIMGPATH=F:\documenti\Progetti\ebook\
    set SOURCEIMGNAME=Ebook058.JPG
		set MAGICKPATH=F:\programmi\grafica\ImageMagick-6.6.6-4\

    rem Each distorted page is defined by TopLeft X,Y , TopRight X,Y, BottomRight X,Y and BottomLeft X,Y  coordinates;

    set TLX1=580
    set TLY1=30
    set TRX1=1250
    set TRY1=195
    set BLX1=580
    set BLY1=1323
    set BRX1=1250
    set BRY1=1264

    rem Right coordinates of left image match to left coordinates of right image:
    set TLX2=1275
    set TLY2=216
    set TRX2=1901
    set TRY2=29
    set BLX2=1293
    set BLY2=1265
    set BRX2=1967
    set BRY2=1302

rem ************ END OF SETTINGS *******************
rem ************************************************


    Rem Calculate page width and height:
    set /A WIDTH1= %TRX1%-%TLX1%
    set /A HEIGHT1= %BLY1%-%TLY1%

    set /A WIDTH2= %TRX2%-%TLX2%
    set /A HEIGHT2= %BRY2%-%TRY2%

    rem Calculate top and bottom offset for right margin of left page:
    set /A TopOffset1 = %TRY1%-%TLY1%
    set /a BottomOffset1 = %TopOffset1%+%BRY1%-%TRY1%

    set /A TopOffset2 = %TRY1%-%TLY1%
    set /a BottomOffset2 = %TopOffset2%+%BLY2%-%TLY2%

    rem Debug printing:
    echo %WIDTH1%x%HEIGHT1%+%TLX1%+%TLY1% %SOURCEIMGPATH%left.jpg
    echo %WIDTH2%x%HEIGHT2%+%TLX2%+%TLY1% %SOURCEIMGPATH%right.jpg
    echo 0,0,1,1   %WIDTH1%,%TopOffset%,%WIDTH1%,0  0,%HEIGHT1%,0,%HEIGHT1%    %WIDTH1%,%BottomOffset%,%WIDTH1%,%HEIGHT1%



    rem ******* Left page ********

    rem Crop left image:
    rem echo ---------- %MAGICKPATH%convert %SOURCEIMGPATH%%SOURCEIMGNAME% -crop %WIDTH1%x%HEIGHT1%+%TLX1%+%TLY1% c:\temp\left.jpg
    %MAGICKPATH%convert %SOURCEIMGPATH%%SOURCEIMGNAME% -crop %WIDTH1%x%HEIGHT1%+%TLX1%+%TLY1% left.jpg

    Rem Adjust left image (fix perspective distortion):
    %MAGICKPATH%convert left.jpg -virtual-pixel white -distort Perspective "0,0,1,1   %WIDTH1%,%TopOffset%,%WIDTH1%,0  0,%HEIGHT1%,0,%HEIGHT1%    %WIDTH1%,%BottomOffset%,%WIDTH1%,%HEIGHT1%" left-ok.jpg


    rem ******** Right page *******

    rem Crop right image:
    %MAGICKPATH%convert %SOURCEIMGPATH%%SOURCEIMGNAME% -crop %WIDTH2%x%HEIGHT2%+%TLX2%+%TLY1% right.jpg

    Rem Adjust right image (fix perspective distortion):
    %MAGICKPATH%convert right.jpg -virtual-pixel white -distort Perspective "0,%TopOffset%,0,0   %WIDTH2%,0,%WIDTH2%,0  %WIDTH2%,%HEIGHT2%,%WIDTH2%,%HEIGHT2%   0,%BottomOffset%,0,%HEIGHT2%" right-ok.jpg

Now, let's think about pages not being exactly straight, but a little bended: how do I manage this? Isn't there any "polyline distortion" available in imagemagick? Splitting a page in a dozen of "straight sectors" does not look like the best solution... :(
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ebook distortion

Post by anthony »

It has not been throughly tested but your could try polynomial. You need a lot of control points though.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: ebook distortion

Post by jumpjack »

Thanks.
I did some test, but with poor results.

How would you transform this image to get straight lines?!?
Image

Uploaded with ImageShack.us


This is the best I can get:
Image

I used this commandline:

convert right.jpg -matte -virtual-pixel white -distort Polynomial "1.5 24,236 24,75 586,75 586,75 623,1181 623,1181 31,1160 31,1182 " right_p.jpg

Using "2" and 6 points rather than "1.5" and 4 points results... in a mess! :?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ebook distortion

Post by anthony »

A 1.5 order polynomial may not be enough. That is only bilinear. You may need a much higher order, 2 many be 3, with a even larger number of control points.

Note you can read the order and control points from a separate text file new of lines juts count as another type of argument separator (along with spaces an commas).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: ebook distortion

Post by jumpjack »

anthony wrote:A 1.5 order polynomial may not be enough. That is only bilinear. You may need a much higher order, 2 many be 3, with a even larger number of control points.

Note you can read the order and control points from a separate text file new of lines juts count as another type of argument separator (along with spaces an commas).
did you read last line of my post?
I tried, with weird results.
In which order the points should be specified?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ebook distortion

Post by fmw42 »

Looks like a combination of barrel and perspective distortion as the edges of the original are not really straight as best as I can tell from your posted image.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ebook distortion

Post by anthony »

Actually I think it is a curve in the paper itself. which is why I do not think barrel will fix the problem. A more general approach is needed, which is why I suggested polynomial.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ebook distortion

Post by fmw42 »

My thought was that the lens is producing barrel distortion and the view was taken in perspective. So I would correct the perspective and then try some barrel correction. Polynomial may work, but requires way too many points and will probably never get it perfectly straight for every line. But it is worth trying.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: ebook distortion

Post by anthony »

You should always correct lens barrel distortion before perspective! But the page distortion could be corrected to some degree with barrel distortion. However working out the parameters would be a real problem! Polynomial at least works with coordinates, rather than distortion parameters.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: ebook distortion

Post by fmw42 »

anthony wrote:You should always correct lens barrel distortion before perspective! But the page distortion could be corrected to some degree with barrel distortion. However working out the parameters would be a real problem! Polynomial at least works with coordinates, rather than distortion parameters.

Yes your are right, the barrel should be first before the perspective.
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: ebook distortion

Post by jumpjack »

anthony wrote:Actually I think it is a curve in the paper itself. which is why I do not think barrel will fix the problem. A more general approach is needed, which is why I suggested polynomial.
I don't think if also barrel distortion is present, but by sure I know that the paper is curved, and the curve radius is not always the same:

Image
Post Reply