Trouble with " cv2.imshow ()" function

19,098

imshow should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won’t display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame). Here's a working example:

import cv2

img = cv2.imread('a.jpg')
cv2.imshow('FRAME', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Share:
19,098
Kushagra
Author by

Kushagra

Updated on June 07, 2022

Comments

  • Kushagra
    Kushagra almost 2 years

    I installed openCV and numpy libraries in python 2.7.

    I've tested them using commands import cv2 and import numpy and it compiled.

    But when I use the cv2.imshow('frame', ----) function it displays a window but not displaying the image. And it's showing " frame is Not Responding".

    So, I tried with matplotlib functions for displaying image and it worked.

    I inserted cv2.imshow function in the 2nd case and it worked.

    Versions [Python-2.7.10, OpenCV-2.4.11]

    Below is the code,

    Case 1: Not Working,displaying window but not image (showing FRAME IS NOT RESPONDING)

    import cv2
    import numpy 
    
    img = cv2.imread('a.jpg')
    cv2.imshow('FRAME',img)
    

    Case 2: Working

    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import numpy as np
    import cv2
    
    img = mpimg.imread('a.jpg')
    img2 = cv2.imread('b.jpg')
    cv2.imshow('FRAME',img2)
    plt.imshow(img)
    plt.show()
    
  • ImportanceOfBeingErnest
    ImportanceOfBeingErnest over 7 years
    This does not solve the question since the problem arises when showing the image, and that is independent of reading it in.