简体   繁体   中英

Trouble with “ cv2.imshow ()” function

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

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

Try using imread like this

img = cv2.imread('a.jpg',0)#grayscale
img = cv2.imread('a.jpg',1)#rgb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM