[英]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
i am trying to rotate an image at 45 degrees in the center of rotation but i am having a problem achieving it in the center by using any two given coordinates such as [0,0]我试图在旋转中心将图像旋转 45 度,但我在使用任何两个给定坐标(例如 [0,0])在中心实现它时遇到问题
expecting a 45 dgrees at the center using any two given coordinates使用任意两个给定坐标,期望在中心有 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)
I'm not sure what's wrong with your code, but here's a simple way of doing it with opencv (you can install it with pip install opencv-python
)我不确定您的代码有什么问题,但这是使用 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.