【问题标题】:How to get a new coordinates after image rotation?图像旋转后如何获得新坐标?
【发布时间】:2019-05-17 21:30:01
【问题描述】:

我正在尝试在图像旋转后获取新坐标。但是,我有一个相对坐标的坐标。所有四个坐标都包含 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))适当计算。但是,输出的值超出了 0 和 1 之间的范围。
  • 这并不异常。

标签: python opencv image-processing


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 2010-10-09
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    相关资源
    最近更新 更多