13
$\begingroup$

I'd like to crop and transform out the magazine cover so that it is rectangular.

enter image description here

I read the docs for ImagePerspectiveTransformation, and here's where I got to. First, I found the bounding polygon top-left top-right, bottom-right and bottom-left points:

img = Import@
"http://www.vinylbanner.com/media/catalog/product/cache/1/image/\
1800x/040ec09b1e35df139433887a97daa66f/s/l/slanted-magic-mount-3.jpg";
corners = {{212.29296875`, 156.265625`}, {594.69921875`, 
  157.5703125`}, {489.16796875`, 424.98828125`}, {236.30859375`, 430.54296875`}};

Assuming its aspect ratio is 11 by 8.5 and it is 100 pixels tall:

h = 100;
w = (8.5/11)*h;
ft = FindGeometricTransform[corners, {{0, h}, {w, h}, {0, w}, {0, 0}}]
new = ImagePerspectiveTransformation[img, ft[[2]]]

but I get an error:

enter image description here

$\endgroup$
2
  • $\begingroup$ Are you sure those corners are coordinates? They more like RGB values. $\endgroup$ Commented Sep 12, 2016 at 5:49
  • $\begingroup$ Yup, sorry bout that, fixed! $\endgroup$
    – user5601
    Commented Sep 12, 2016 at 5:51

1 Answer 1

22
$\begingroup$

A few minor mistakes:

  • corners and the target coordinates weren't in the same order
  • For some reasons, pixels isn't the default unit for ImagePerspectiveTransformation and friends - you have to specify PlotRange and DataRange explicitly
  • the target coordinates should go first in FindGeometricTransform - alternatively, you can pass InverseFunction@ft[[2]] to ImagePerspectiveTransformation

Which leads to this result:

h = 300;
w = (8.5/11)*h;
ft = FindGeometricTransform[{{0, 0}, {w, 0}, {w, h}, {0, h}}, corners]
new = ImagePerspectiveTransformation[img, ft[[2]], 
  PlotRange -> {{0, w}, {0, h}}, DataRange -> Full]

enter image description here

$\endgroup$
1
  • $\begingroup$ @nikkie thanks! $\endgroup$
    – user5601
    Commented Sep 12, 2016 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.