在OpenCV中画一条线不起作用

时间:2015-02-03 09:06:06

标签: python-2.7 opencv drawing 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
        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()

1 个答案:

答案 0 :(得分:3)

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()