簡體   English   中英

我希望能夠配置以任何坐標為中心的任何旋轉角度,並在中心獲得 45 度的預期結果

[英]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 度,但我在使用任何兩個給定坐標(例如 [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)

我不確定您的代碼有什么問題,但這是使用 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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM