【问题标题】:Rotating points and drawing with cv2.polylines使用 cv2.polylines 旋转点和绘图
【发布时间】:2018-08-15 10:29:27
【问题描述】:

我无法正确围绕某个点进行旋转。

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

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


    【解决方案1】:

    在第二行中,您使用 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)
    

    【讨论】:

    • 天哪,我不敢相信自己的愚蠢,哈哈……这就是你为走捷径付出的代价,我猜!谢谢 x999999 !
    • 重复使用相同的变量是邪恶的;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多