【问题标题】:Rotate polygons without cutting edges旋转多边形而不使用切削刃
【发布时间】:2021-10-28 16:52:22
【问题描述】:

我正在编写一个增强代码来旋转图像内的带注释的多边形。我写了一个代码,但它不能正常工作。只需复制粘贴代码即可获得结果。谢谢你帮助我。

图片:

需要为各个角度旋转图像以及多边形。目前,我无法旋转多边形

图像已旋转,但多边形仍在原位。

我试过这段代码。它旋转多边形但不在正确的位置

import math
from PIL import Image, ImageDraw
from PIL import ImagePath 
from PIL import Image
import matplotlib.pyplot as plt
from math import sin, cos, radians
import requests
from io import BytesIO

def rotatePolygon(polygon, degrees, height, width):
    """ 
    Description:
    
        Rotate polygon the given angle about its center. 
        
    Input:
        polygon (list of tuples)  : list of tuples with (x,y) cordinates 
                                    e.g [(1,2), (2,3), (4,5)]
        
        degrees int               : Rotation Degrees
    
    Output:
    
        polygon (list of tuples)  : Polygon rotated on angle(degrees)
                                e.g [(1,2), (2,3), (4,5)]
    
    """
    # Convert angle to radians
    theta = radians(degrees)  
    
    # Getting sin and cos with respect to theta
    cosang, sinang = cos(theta), sin(theta) 

    # find center point of Polygon to use as pivot
    y, x = [i for i in zip(*polygon)]
    
    # find center point of Polygon to use as pivot
    
    cx = width / 2
    cy = height / 2
    
    # Rotating every point
    new_points = []
    for x, y in zip(x, y):
        tx, ty = x-cx, y-cy
        new_x = (tx*cosang + ty*sinang) + cx
        new_y = (-tx*sinang + ty*cosang) + cy
        new_points.append((new_y, new_x))
    return new_points



# Polygon
xy = [(85, 384), (943, 374), (969, 474), (967, 527), (12, 540), (7, 490)]
degrees = 270


# Getting Image from URL
try:
    img = Image.open("polygon_image.png")
except:
    url = "https://github.com/SohaibAnwaar/Mask---RCNN-Polygons-/blob/main/2_image_augmentation/extras/problamatic_image.jpg?raw=true"
    response = requests.get(url)
    img = Image.open(BytesIO(response.content))
    img.save("polygon_image.png")



# Rotating Image
rotated_image = img.rotate(degrees,expand = True)
h, w = img.size

print("NotRotated", xy)
rotated_xy = rotatePolygon(xy, 360 - (degrees), h, w)



# Ploting Rotated Image
img1 = ImageDraw.Draw(rotated_image)  
img1.polygon(rotated_xy, fill ="#FFF000", outline ="blue") 


# Ploting Straight Image
img1 = ImageDraw.Draw(img)  
img1.polygon(xy, fill ="#FFF000", outline ="blue") 

plt.imshow(rotated_image)
plt.show()


plt.imshow(img)
plt.show()

【问题讨论】:

  • 我试过你的答案,但没有帮助我。旋转多边形时,边缘仍在切割
  • 它使用 rotate_image = img.rotate(degrees,expand = False) 但我需要使用 rotate_image = img.rotate(degrees,expand = True) 完成它
  • 这不是你的答案,但是;您不能在显示原始图像之前在原始图像内绘制一个多边形,然后旋转该图像吗?在这种情况下,多边形将在其原始位置旋转。旋转后需要多边形坐标吗?如果不是,这不是一个困难的解决方案。
  • 是的,我需要多边形坐标。在图像上绘制多边形并获得旋转的多边形蒙版很容易。但我需要用数学方程旋转所有多边形

标签: python image-processing computer-vision object-detection data-augmentation


【解决方案1】:

旋转方程为:

xnew = x * cos(theta) - y * sin(theta)
ynew = x * sin(theta) + y * cos(theta)

您正在做的唯一错误是:

new_x = (tx*cosang - ty*sinang) + cy
new_y = (tx*sinang + ty*cosang) + cx

旋转图像后,cx和cy应该改变

您的完整代码如下:

import math
import numpy as np
from PIL import Image, ImageDraw
from PIL import ImagePath 
from PIL import Image
import matplotlib.pyplot as plt
from math import sin, cos, radians
import requests
from io import BytesIO

def rotatePolygon(polygon, degrees, height, width):
    """ 
    Description:
    
        Rotate polygon the given angle about its center. 
        
    Input:
        polygon (list of tuples)  : list of tuples with (x,y) cordinates 
                                    e.g [(1,2), (2,3), (4,5)]
        
        degrees int               : Rotation Degrees
    
    Output:
    
        polygon (list of tuples)  : Polygon rotated on angle(degrees)
                                e.g [(1,2), (2,3), (4,5)]
    
    """
    # Convert angle to radians
    theta = radians(degrees)
    
    # Getting sin and cos with respect to theta
    cosang, sinang = cos(theta), sin(theta) 

    # find center point of Polygon to use as pivot
    y, x = [i for i in zip(*polygon)]
    
    # find center point of Polygon to use as pivot
    
    cx1 = width[0] / 2
    cy1 = height[0] / 2
    cx2 = width[1] / 2
    cy2 = height[1] / 2
    
    # Rotating every point
    new_points = []
    for x, y in zip(x, y):
        tx, ty = x-cx1, y-cy1
        new_x = (tx*cosang - ty*sinang) + cx2
        new_y = (tx*sinang + ty*cosang) + cy2
        new_points.append((new_y, new_x))
    return new_points



# Polygon
xy = [(85, 384), (943, 374), (969, 474), (967, 527), (12, 540), (7, 490)]
degrees = 270


# Getting Image from URL
try:
    img = Image.open("polygon_image.png")
except:
    url = "https://github.com/SohaibAnwaar/Mask---RCNN-Polygons-/blob/main/2_image_augmentation/extras/problamatic_image.jpg?raw=true"
    response = requests.get(url)
    img = Image.open(BytesIO(response.content))
    img.save("polygon_image.png")



# Rotating Image
rotated_image = img.rotate(degrees,expand = True)
h1, w1 = img.size
h2, w2 = rotated_image.size

print("NotRotated", xy)
rotated_xy = rotatePolygon(xy, degrees, [h1,h2], [w1,w2])



# Ploting Rotated Image
img1 = ImageDraw.Draw(rotated_image)  
img1.polygon(rotated_xy, fill ="#FFF000", outline ="blue") 


# Ploting Straight Image
img1 = ImageDraw.Draw(img)  
img1.polygon(xy, fill ="#FFF000", outline ="blue") 

plt.imshow(rotated_image)
plt.show()


plt.imshow(img)
plt.show()

【讨论】:

  • 你试过了吗?因为它没有帮助我。
  • 是的,它对我来说很好用。我也可以上传整个代码。
  • 请上传,我会非常感谢你
  • 试过但仍然有效,在 180 ,45, 90, 270 上试一试,
  • 让我再检查一次。 180我没试过。
【解决方案2】:

要向右旋转一个直角,请使用以下等式(假设X/U从左到右和Y/V自上而下):

U = H - Y
V = X

图片大小为W x H。

【讨论】:

  • 但是我必须把这个等式放在哪里?
  • @SohaibAnwaar:当然是旋转方程!?
  • 对不起!但我无法理解。能不能说的详细一点,谢谢。需要解决这个问题。
  • @SohaibAnwaar:嗯,你的旋转方程目前在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
  • 2021-12-18
  • 2013-03-21
  • 1970-01-01
相关资源
最近更新 更多