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

前端 未结 16 1556
轻奢々
轻奢々 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()
    
    0 讨论(0)
  • 2020-12-04 17:45

    If you choose to use "cv2.waitKey(0)", be sure that you have written "cv2.waitKey(0)" instead of "cv2.waitkey(0)", because that lowercase "k" might freeze your program too.

    0 讨论(0)
  • 2020-12-04 17:46

    add cv2.waitKey(0) in the end.

    0 讨论(0)
  • 2020-12-04 17:50

    I found the answer that worked for me here: http://txt.arboreus.com/2012/07/11/highgui-opencv-window-from-ipython.html

    If you run an interactive ipython session, and want to use highgui windows, do cv2.startWindowThread() first.

    In detail: HighGUI is a simplified interface to display images and video from OpenCV code. It should be as easy as:

    import cv2
    img = cv2.imread("image.jpg")
    cv2.startWindowThread()
    cv2.namedWindow("preview")
    cv2.imshow("preview", img)
    
    0 讨论(0)
  • 2020-12-04 17:52

    This is how I solve it

    import cv2
    from matplotlib import pyplot
    
    img = cv2.imread('path')
    pyplot.imshow(img)
    pyplot.show()
    
    0 讨论(0)
  • 2020-12-04 17:53

    You must use cv2.waitKey(0) after cv2.imshow("window",img). Only then will it work.

    import cv2
    img=cv2.imread('C:/Python27/03323_HD.jpg')
    cv2.imshow('Window',img)
    cv2.waitKey(0)
    
    0 讨论(0)
提交回复
热议问题