【问题标题】:I want to be able to configure any rotation angle centered at any coordinate and have the expected result of 45 degrees at the center我希望能够配置以任何坐标为中心的任何旋转角度,并在中心获得 45 度的预期结果
【发布时间】:2022-12-20 00:25:26
【问题描述】:

我试图在旋转中心将图像旋转 45 度,但我在使用任何两个给定坐标(例如 [0,0])在中心实现它时遇到问题

使用任意两个给定坐标,期望在中心有 45 度角

#Pivot is the coordonates for the center of rotation
def rotateImage(self, img, angle, pivot):
    padX = int(pivot[1] - img.shape[1] / 2)
    padY = int(pivot[0] - img.shape[0] / 2)
    x1, x2, y1, y2 = 0,0,0,0
    if(pivot[1] > img.shape[1]/2):
        img = pad(img, ((0,0),(0, padX)), 'constant', constant_values=1)
        x2=padX
    elif(pivot[1] < img.shape[1]):
        img = pad(img, ((0,0),(abs(padX), 0)), 'constant', constant_values=1)
        x1=abs(padX)

    if (pivot[0] > img.shape[0] / 2):
        img = pad(img, ((0, padY), (0, 0)), 'constant', constant_values=(1,1))
        y2=padY
    elif (pivot[0] < img.shape[0]):
        img = pad(img, ((abs(padY), 0), (0, 0)), 'constant', constant_values=(1,1))
        y1=abs(padY)

    imgR = ndimage.rotate(img, angle, reshape=False, cval=1)
    return imgR[y1: imgR.shape[0]-y2, x1: imgR.shape[1]-x2]
try:
    angle = float(self.rotation_value.toPlainText())
except:
    angle = 0
if (angle <= 360 and angle >= -360 and angle != 0):
    corrected = self.rotateImage(corrected, angle, [0, 0])

writeraw32(self.path_ImageJ + "/temp/LE_corr_rot.raw", corrected)

【问题讨论】:

    标签: python rotation angle degrees


    【解决方案1】:

    我不确定您的代码有什么问题,但这是使用 opencv 执行此操作的简单方法(您可以使用 pip install opencv-python 进行安装)

    import cv2
    import matplotlib.pyplot as plt
    
    
    # Load the image
    image = cv2.imread('image.png')
    
    # Define the angle of rotation (in degrees) and the center of rotation
    angle = 45
    center = (10, 10)
    
    # Calculate the rotation matrix
    rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1)  # 1 is the scale
    
    # Rotate the image
    rotated_image = cv2.warpAffine(image, rotation_matrix, image.shape[:2])
    
    # Display the image - or write the image with your code
    plt.imshow(rotated_image)
    plt.show()
    
    

    【讨论】:

      最近更新 更多