如何将圆形图像裁剪为内切正方形,然后裁剪为内切圆,最后裁剪为内切正方形?

时间:2018-07-04 01:22:46

标签: python image opencv image-processing python-imaging-library

我想根据以下内容裁剪以下所附的圆形图像:

  1. 将输入的圆形图像裁剪到圆内刻的唯一正方形上。
  2. 将正方形图像裁剪到正方形内切圆
  3. 将上一步中的圆形图像裁切成刻有图像的正方形。

我正在使用python,opencv和PIL。我曾尝试使用硬编码坐标进行裁剪,但这显然在应用于其他图像时会引起问题,因此我需要一个通用解决方案。

我已经粗略地看到了我想如何修剪它:

此外,我还包含原始图像:

一个带有简短说明的代码片段将不胜感激。

2 个答案:

答案 0 :(得分:2)

要裁剪的图像在几何上是以输入图像为中心的正方形,大小为一半。这是因为您要刻两次,每次平方缩小为2的平方根,然后除以SQRT(2)两次就等于除以2。

因此,如果您有D边的输入正方形(或直径D的圆形图像),则需要做的是以中心(D / 2,D / 2)和D / 2的边进行裁切。 / p>

答案 1 :(得分:1)

您不需要进行中间裁剪。

只需计算结果平方参数。

类似这样的伪代码(我在c ++中更好):

def calcSideOfInscribedSquare(current_size):
    return current_size / sqrt(2)

initial_side = min(image.cols, image.rows)
first_squre_side = calcSideOfInscribedSquare(initial_side)
second_square_side = calcSideOfInscribedSquare(first_squre_side)

cv.Rect result_square(
    image.cols / 2 - second_square_side / 2,
    image.rows / 2 - second_square_side / 2,
    second_square_side,
    second_square_side)

new_image = image(result_square)