簡體   English   中英

圖像旋轉后如何獲取新坐標?

[英]How to get a new coordinates after image rotation?

我正在嘗試在圖像旋轉后獲取新的坐標。 但是,我有一個相對坐標。 所有四個坐標均由介於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);

暫無
暫無

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

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