3 # Convert quadrilateral to square
8 $src->open(file=>"oldmap_200px.jpg");
10 # example co-ordinates of quadrilateral
11 my $x0=12; my $y0=4; # top left
12 my $x1=157; my $y1=0; # top right
13 my $x2=140; my $y2=150; # bottom right
14 my $x3=27; my $y3=159; # bottom left
17 xa=((h-y)*x0+y*x3)/h; ya=((h-y)*y0+y*y3)/h;
18 xb=((h-y)*x1+y*x2)/h; yb=((h-y)*y1+y*y2)/h;
19 xc=((w-x)*x0+x*x1)/w; yc=((w-x)*y0+x*y1)/w;
20 xd=((w-x)*x3+x*x2)/w; yd=((w-x)*y3+x*y2)/w;
22 d=det(xa-xb,ya-yb,xc-xd,yc-yd);
25 px=det(det(xa,ya,xb,yb),xa-xb,det(xc,yc,xd,yd),xc-xd)/d;
26 py=det(det(xa,ya,xb,yb),ya-yb,det(xc,yc,xd,yd),yc-yd)/d;
30 my $newimg=Imager::transform2({
34 constants=>{x0=>$x0,y0=>$y0,
39 $newimg->write(file=>"output_imager.jpg");
43 quad_to_square.pl - transform an arbitrary quadrilateral to a square.
47 perl quad_to_square.pl
51 =for stopwords Fairhurst resized
53 Courtesy Richard Fairhurst:
55 I've been using it to rectify ("square up") a load of roughly scanned
56 maps, so that an arbitrary quadrilateral is resized into a square. The
57 transform2 function is ideal for that.
59 Thought you might be interested to see what people are doing with
60 Imager - feel free to include this in the sample code.