2
$\begingroup$

I'm cropping a rotated image, but I want to keep the ascect ratio fixed and not have any "extra" black pixels. Here's an example:

image = ExampleData[{"TestImage", "House"}];
rot = ImageRotate[image, 5 Degree]
ImageCrop[rot, {250, 200}]

enter image description here

But this isn't a good solution because it specifies the exact crop and I'm not sure what that will be generally. So really, what I want is the take a big of a crop as possible, preserving some given aspect ratio (which by default would be the same as the original image's asepect).

I would manually do it like this:

enter image description here

Details

$\endgroup$
  • $\begingroup$ How do you feel about computing it from the morphological components? How fast does this need to be? One other option is to first pad with a very specific random color then find the bounding pixels with that color from the image data. $\endgroup$ – b3m2a1 Sep 18 '17 at 20:50
  • $\begingroup$ @b3m2a1 We should be able to compute it with basic geometry no? $\endgroup$ – M.R. Sep 18 '17 at 20:50
  • $\begingroup$ Oh if you know the rotation angle, sure. Take the corner points of a rectangle, rotate those, take the second from smallest left, second from largest right, or something. I was assuming you had randomly rotated images. $\endgroup$ – b3m2a1 Sep 18 '17 at 20:51
  • $\begingroup$ @b3m2a1 I don't think its that easy. See stackoverflow.com/questions/16702966/… $\endgroup$ – M.R. Sep 18 '17 at 20:55
  • $\begingroup$ Oh I missed the AspectRatio restriction. I see the complication now. $\endgroup$ – b3m2a1 Sep 18 '17 at 21:09
6
$\begingroup$

Simply use the option "SameRatioCropping" in ImageRotate :

image = ExampleData[{"TestImage", "House"}];
ImageRotate[image, 5 Degree,"SameRatioCropping"]

enter image description here

"SameRatioCropping" is documented. There is also a good explanation in this Almsick's presentation, at time 10 minutes

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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