【发布时间】:2018-08-15 10:29:27
【问题描述】:
我无法正确围绕某个点进行旋转。
我想要实现的是一个将正方形的 4 个角围绕其中心旋转的功能。然后我显示旋转的正方形。我需要这个来测试其他一些功能。
现在发生的情况是点之间的距离没有保留。对于pi/2 I get a diagonal 和pi/5 a "diamond"。
我认为这可能与 imshow 使用的坐标系中的 y 轴被翻转(向下增加值)这一事实有关。我玩过标志,但没能把它弄好。请帮我找出错误!
## Define corners of a square
p1 = [200,200] #[x,y]
p2 = [200,300]
p3 = [300,300]
p4 = [300,200]
## Choose the angle of rotation
rotAng = math.pi/3
## Rotate the corners
p1 = rotPt(p1, rotAng)
p2 = rotPt(p2, rotAng)
p3 = rotPt(p3, rotAng)
p4 = rotPt(p4, rotAng)
## Display square as a polyline
img = np.zeros((640,480,3), np.uint8) #creates an empty image
pts = np.array([p1,p2,p3,p4], np.int32)
pts = pts.reshape((-1,1,2))
img = cv2.polylines(img,[pts],True,(255,255,255), thickness = 3)
## View the image
depth_image = np.asanyarray(img)
cv2.imshow('depth_image', depth_image)
def rotPt((x, y), th):
cx = 250
cy = 250 #centre of square coords
x -= cx
y -= cy
x = x*math.cos(th) - y*math.sin(th)
y = x*math.sin(th) + y*math.cos(th)
x += cx
y += cy
return [x,y]
【问题讨论】:
标签: python opencv math rotation computer-vision