gpt4 book ai didi

python - 图像旋转后如何获得新的坐标?

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:27 26 4
gpt4 key购买 nike

我正在尝试在图像旋转后获取新坐标。但是,我有一个坐标是相对坐标。所有四个坐标都由 0 到 1 之间的值组成。例如 (x1, y1) = [0.15, 0.15] (x2, y2) = [0.8, 0.15] (x3, y3) = [0.8, 0.8] (x4, y4) = [0.15, 0.8]当我将图片旋转 n 度时,我想得到新的 x、y 坐标。

image = Image.open(os.path.join('./AlignImages', image_name))

labels = np.array(list(map(float, a.split(" ")[1:]))).astype('float32')
#if labels == [0.1 0.1 0.5 0.1 0.5 0.5 0.1 0.5] [x1 y1 x2 y2 x3 y3 x4 y4]
labels = np.vstack((labels[0::2], labels[1::2]))
# [0.1 0.5 0.5 0.1] [x1 x2 x3 x4]
# [0.1 0.1 0.5 0.5] [y1 y2 y3 y4]
print(labels)

labels = np.array([[labels[0][0]-0.5, labels[0][1]-0.5, labels[0][2]-0.5, labels[0][3]-0.5],[0.5-labels[1][0], 0.5-labels[1][1], 0.5-labels[1][2], 0.5-labels[1][3]]])
#This is to move the center point of the image.
#Adjust the value to rotate because the upper left corner of the image is (0, 0)
image = image.rotate(rotation_scale, expand=True)
#I gave the option to expand the image so that the rotated image was not cropped.

image.show()
rotation_ = np.array([[np.cos(rotation_scale), (np.sin(rotation_scale))],[-1*np.sin(rotation_scale), np.cos(rotation_scale)]])
#I have defined a transformation matrix.

src = np.matmul(rotation_, labels)
#Multiply the transformation matrix by the coordinates to obtain the new coordinates.


src = np.array([[src[0][0]+0.5, src[0][1]+0.5, src[0][2]+0.5, src[0][3]+0.5],[0.5+src[1][0], 0.5+src[1][1], 0.5+src[1][2], 0.5+src[1][3]]])
#Let the top left corner be 0, 0 again.

print(src)

[[ 0.24779222  1.00296445  0.7265248  -0.05902794]
[ 0.8065444 0.41615766 0.2350563 0.60667523]]

但是,这段代码似乎不起作用。我以为我可以在该源代码中获得旋转图像的四个相对坐标,但根本没有。我想获取展开图像(旋转图像)中四个顶点的相对坐标。这些值都应该在 0 和 1 之间。如何获得我想要的四个坐标?

最佳答案

问题可能出在您围绕旋转点的中心点。根据我上一个项目的经验,你需要知道中心点和度数

例如:您的图像围绕中心点(图像的中间点)向右旋转了 90 度,现在您需要将这些点围绕中心点向后旋转 -90 度。 c++ 中的代码(很抱歉,我只熟悉 c++,但我认为您可以轻松移植到 python)

// the center point 
Point2f center=(width/2,height/2)

//the angle to rotate, in radiant
// in your case it is -90 degree
double theta_deg= angleInDegree * 180 /M_PI;

// get the matrix to rotate
Mat rotateMatrix = getRotationMatrix2D(center, theta_deg, 1.0);

// the vector to get landmark points
std::vector<cv::Point> inputLandmark;
std::vector<cv::Point> outputLandmark;

// we use the same rotate matrix and use transform
cv::transform(inputLandmark, outputLandmark, rotateMatrix);

关于python - 图像旋转后如何获得新的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56186963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com