简体   繁体   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

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.

相关问题 任意度数的 Numpy 矩阵旋转 - Numpy matrix rotation for any degrees pandas function any() 不返回我想要的结果 - the pandas function any() don’t return the result what i want python plotly express mapbox:在map上配置自定义45度角 - python plotly express mapbox: configure custom 45 degree angle on map 检查列表中的任何精灵是否超过某个x坐标? - Check if any sprites in a list have gone past a certain x coordinate? 嗨,我想在浏览器堆栈中运行 selenium python 脚本,并想在移动设备中检查任何人有任何解决方案我们怎么能 - hi i want to run selenium python script in browser stack and want to check in mobile any one have any solution how we can 我想从数据集创建列表并在 palantir 铸造厂的另一个 function 中使用,但找不到任何解决方案 - I want to create the list from the datasest and use in another function in palantir foundry but not able to find any solution 我可以将x轴旋转45度吗? 试图使象形图 - Can I rotate the x axis by 45 degrees? Trying to make tephigram 如何以任意角度绘制文本并平行于一条线? - How can I draw text at any angle above and parallel to a line? 我有一个指向要下载的 .mp4 文件的链接。 但在我下载它之前,我希望它被压缩。 有什么建议吗? - I have a link to a .mp4 file I want to download. But before I download it I want it compressed. Any advice? 给定三个坐标点,如何检测它们之间的角度何时超过180度? - Given three coordinate points, how do you detect when the angle between them crosses 180 degrees?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM