cv2.imshow command doesn't work properly in opencv-python

前端 未结 16 1647
轻奢々
轻奢々 2020-12-04 17:31

I\'m using opencv 2.4.2, python 2.7 The following simple code created a window of the correct name, but its content is just blank and doesn\'t show the image:



        
16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 17:44

    Method 1:

    The following code worked for me. Just adding the destroyAllWindows() didn't close the window. Adding another cv2.waitKey(1) at the end did the job.

    im = cv2.imread("./input.jpg")
    cv2.imshow("image", im)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.waitKey(1)
    

    credit : https://stackoverflow.com/a/50091712/8109630

    Note for beginners:

    • This will open the image in a separate window, instead of displaying inline on the notebook. That is why we have to use the destroyAllWindows() to close it later.
    • So if you don't see a separate window pop up, check if it is behind your current window.
    • After you view the image press a key to close the popped up window.

    Method 2:

    If you want to display on the Jupyter notebook.

    from matplotlib import pyplot as plt
    import cv2
    
    im = cv2.imread("./input.jpg")
    color = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    plt.imshow(color)
    plt.title('Image')
    plt.show()
    

提交回复
热议问题