Page 1 of 2
ebook distortion
Posted: 2010-12-10T06:33:04-07:00
by jumpjack
I want to perform such a transformation on an 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?
Re: ebook distortion
Posted: 2010-12-10T07:32:23-07:00
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.
Re: ebook distortion
Posted: 2010-12-10T08:38:28-07:00
by jumpjack
They're "quite" straight (I can consider them as straight, at least for now)
Re: ebook distortion
Posted: 2010-12-10T09:18:19-07:00
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?
Re: ebook distortion
Posted: 2010-12-11T10:39:46-07:00
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...
Re: ebook distortion
Posted: 2010-12-11T19:05:21-07:00
by anthony
It has not been throughly tested but your could try polynomial. You need a lot of control points though.
Re: ebook distortion
Posted: 2010-12-12T04:41:54-07:00
by jumpjack
Thanks.
I did some test, but with poor results.
How would you transform this image to get straight lines?!?
Uploaded with
ImageShack.us
This is the best I can get:
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!
Re: ebook distortion
Posted: 2010-12-12T04:48:34-07:00
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).
Re: ebook distortion
Posted: 2010-12-12T05:18:06-07:00
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?
Re: ebook distortion
Posted: 2010-12-12T13:44:25-07:00
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.
Re: ebook distortion
Posted: 2010-12-12T17:53:08-07:00
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.
Re: ebook distortion
Posted: 2010-12-12T18:01:09-07:00
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.
Re: ebook distortion
Posted: 2010-12-12T18:08:59-07:00
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.
Re: ebook distortion
Posted: 2010-12-12T20:02:06-07:00
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.
Re: ebook distortion
Posted: 2010-12-13T00:21:28-07:00
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: