Reputation: 7380
I've submitted this as a potential bug on OpenImaj's github issues board, but wanted to reach out on SO and see if this has been encountered before (it's not an obscure class / method and I would be surprised if I am the first to come across this issue). I realize this is less of a question and more of a raised issue, but this would warrant comment from others in their experience with this method.
https://github.com/openimaj/openimaj/issues/86
While attempting to resize photo's of various uncontrolled sizes, I encountered a divide by zero exception. The relevant portion of the stacktrace is below:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at org.openimaj.image.processing.resize.ResizeProcessor.resizeMaxArea (ResizeProcessor.java:443)
Upon further review, it appears to only occur when the image height is greater than the width. In such a circumstance, the whRatio float ends up being 0 as it performs integer division on line 441. That then causes newWidth on line 442 to be 0 which then causes the exception on line 443.
I don't know anything about the algorithm in general, so I don't know if this change produces correct values (I'm inclined to think not due to changes made to images via this method result in loss of aspect ratio), however if you cast the width / height division to float prior to setting the result to whRatio, it no longer produces a 0 value (see code below).
final int width = 2687;
final int height = 3356;
final int area = width * height;
final int maxArea = 3700000;
final float whRatio = (float) width / height;
System.out.println("whRatio: " + whRatio);
final double newWidth = Math.sqrt(maxArea * whRatio);
System.out.println("New width: " + newWidth);
Upvotes: 1
Views: 80