使用 cv2.polylines 旋转点和绘图

Rotating points and drawing with cv2.polylines

我在围绕一个点正确旋转时遇到问题。

我想实现的是一个正方形的4个角围绕中心旋转的功能。然后我显示旋转的正方形。我需要这个来测试其他一些功能。

现在发生的是点之间的距离没有保留。对于 pi/2 I get a diagonal and for 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]

在第二行中,您使用更改了 x 的值。

x = x*math.cos(th) - y*math.sin(th)
y = x*math.sin(th) + y*math.cos(th)

记住并使用旧值。

xx = x
x = x*math.cos(th) - y*math.sin(th)
y = xx * math.sin(th) + y*math.cos(th)