Search code examples
pythonopencvartificial-intelligence

Problem getting image to display using imshow (OpenCV)


I am new to opencv and am running the most basic line of code using opencv which is shown below. I have the jpg file in the proper directory and everything. I am using atom as my development environment. However, when I run the code on either the regular shell on windows or the anaconda command prompt shell the image I am trying to open using cv2.imshow('result', image) displays and then stops displaying for such a brief period of time that I cannot even see the image, almost like an instant flash, and then the command prompt goes to the next line indicating the code has been executed. From what I understand the code below should display the image continuously until I close the image or press some other key on my keyboard. No error messages have been displayed and I pip installed opencv properly. I am wondering if there is something I am not seeing that is causing this problem in either my code or something else I haven't tried. Any help would be appreciated. Thank you.

import cv2

image = cv2.imread('Image/test_image.jpg')
cv2.imshow('result',image)
cv2.waitkey(0)

Solution

  • I figured out your problem. It happens while using the cv2.waitkey() method. "k" letter should be capital. use as:

    cv2.waitKey(0) 
    

    Also, try to use like this code below. There is no problem with your configuration environment.

    # importing cv2  
    import cv2  
      
    # Reading an image in default mode 
    image = cv2.imread("lenna.jpg") 
      
    # Using cv2.imshow() method  
    # Displaying the image  
    cv2.imshow('image', image) 
      
    #waits for the user to press any key  
    #(this is necessary to avoid Python kernel form crashing) 
    cv2.waitKey(0)  
      
    #closing all open windows  
    cv2.destroyAllWindows()  
    

    If you have any questions feel free to ask If else give 👍