【问题标题】:Draw a line in OpenCV does not work在 OpenCV 中画线不起作用
【发布时间】:2015-02-03 17:22:48
【问题描述】:

当我运行这个应该在黑色表面上画一条线的代码时,我没有收到任何错误消息,但也没有显示任何内容。怎么了?

import numpy as np 
import cv2

class DessinerLigne:
    def dessinerLigne(self):
        # Create a black image
        self.img=np.zeros((512,512,3),np.uint8)

        # Draw a diagonal blue line with thickness of 5 px
        self.img=cv2.line(self.img,(0,0),(511,511),(255,0,0),5)

        # If q is pressed then exit program
        self.k=cv2.waitKey(0)
        if self.k==ord('q'):
            cv2.destroyAllWindows()

if __name__=="__main__":
    DL=DessinerLigne()
    DL.dessinerLigne()

【问题讨论】:

  • 如何在python中显示图像?我猜在waitKey 之前缺少cv2.imshow("windowname", self.img) 或类似的东西?
  • 以上代码仅适用于 opencv3.0,2.4 版本不会从 cv2.line() 返回图像 [对,缺少 imshow() 你什么都看不到]
  • @berak 我可以在 Python 2.7.9 中使用 OpenCV3.0 吗?

标签: python-2.7 opencv drawing line


【解决方案1】:

OpenCV doc,您可以看到 cv2.line() 什么都不返回,而是在原地运行。

所以你的代码可以是

import numpy as np 
import cv2

class DessinerLigne:
    def dessinerLigne(self):
        # Create a black image
        self.img=np.zeros((512,512,3),np.uint8)

        # Draw a diagonal blue line with thickness of 5 px
        cv2.line(self.img,(0,0),(511,511),(255,0,0),5)
        cv2.imshow("Image", self.img)
        # If q is pressed then exit program
        self.k=cv2.waitKey(0)
        if self.k==ord('q'):
            cv2.destroyAllWindows()

if __name__=="__main__":
    DL=DessinerLigne()
    DL.dessinerLigne()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 2014-06-27
    • 2013-05-17
    • 2011-11-17
    • 1970-01-01
    • 2017-02-09
    • 2015-08-25
    相关资源
    最近更新 更多