我正在尝试在图像旋转后获取新的坐标。但是,我有一个相对坐标。所有四个坐标均由介于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之间。 如何获得所需的四个坐标?
答案 0 :(得分:0)
问题可能出在旋转点周围的中心点上。根据我在上一个项目中的经验,您需要知道中心点和度数
例如:您的图像围绕中心点(图像的中间点)向右旋转了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);