Trouble with “ cv2.imshow ()” function

别等时光非礼了梦想. 提交于 2021-02-05 05:45:09

问题


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

回答1:


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



回答2:


Try using imread like this

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


来源:https://stackoverflow.com/questions/41067007/trouble-with-cv2-imshow-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!