user4519127
user4519127

Reputation: 497

Draw a line in OpenCV does not work

When I run this code which is supposed to draw a line on a black surface, I do not get any error messages but nothing is shown either. What's wrong ?

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

Upvotes: 3

Views: 6767

Answers (1)

tomriddle_1234
tomriddle_1234

Reputation: 3213

From OpenCV doc, you can see cv2.line() returns nothing, but operates in-place.

So your code can be

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

Upvotes: 4

Related Questions