一个旋转的图像最小的边界框 [英] Smallest possible bounding box for a rotated image

查看:211
本文介绍了一个旋转的图像最小的边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个宽一些任意的输入图像的 W1 的和高度的 H1 的。我想所有的方式旋转这个图片回到起始位置360度左右。然而,如果图像是任何东西,除了一个圆,则图像的边缘将被如果它被绘制到工作区保持在大小的 W1 的通过的 H1 的裁剪。

Suppose I have some arbitrary input image with width w1 and height h1. I want to rotate this image all the way around 360 degrees back to the starting position. However, if the image is of anything except a circle, then the edges of the image will be clipped if the canvas it is drawn onto remains at size w1 by h1.

我需要则是确定画布大小(宽度的 W2 的和高度的 H2 的),可以用于所有旋转输入图像的版本。我知道的 W2 的==的 H2 的,因而所需的画布的大小是一个正方形,因为很明显,我们正在旋转一些关于中心点,和最后的图像(在360转),本质上是一个圆。

What I need then is to determine the canvas size (width w2 and height h2) that can be used for all rotated versions of the input image. I know that w2 == h2, and thus the desired canvas size is a square, because it is obvious that we are rotating something about a center point, and the final image (after 360 rotations) is essentially a circle.

另外要考虑的是,对象如广场将有角,将伸出,所以只使用宽度或高度的最大值为两​​个维度也不起作用。

The other thing to consider is that objects such as squares will have corners that will stick out, so just using the max value of width or height for both dimensions also does not work.

我想出了一个解决办法是通过设置来创建画布大于我需要(比如 W2 H2 MAX( W1,H1)* 2 ,旋转的一切,然后修剪所有透明的像素,这是不是很有效,我宁愿能够计算出最紧密的边界框的前期。

One solution I have come up with is to create the canvas larger than I need (for example by setting w2 and h2 to max(w1, h1) * 2, rotating everything, and then trimming all the transparent pixels. This is not very efficient and I would much rather be able to calculate the tightest bounding box upfront.

推荐答案

这是一个几何问题。你基本上是想找到一个圆圈,将写上您的原始帆布和直径(D)则W2 = H2 = D

This is a geometry question. You essentially want to find the diameter (d) of a circle that would inscribe your original canvas and then w2 = h2 = d

这样的圆的直径将√(W1 ^ 2 + H1 ^ 2)

所以 W2 = H2 =√(W1 ^ 2 + H1 ^ 2)

此外,为避免削波,你可能要采取这一结果的上限,而不是四舍五入。

Also, to avoid clipping, you might want to take the ceiling of that result rather than rounding.

这篇关于一个旋转的图像最小的边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆