gpt4 book ai didi

python - Opencv imshow 不创建窗口

转载 作者:太空宇宙 更新时间:2023-11-03 22:09:37 24 4
gpt4 key购买 nike

我最近在使用 Debian 操作系统的树莓派上安装了 opencv 2.4.9 和 python。我写了下面的简单代码来显示图像

import numpy as np
import cv2


print "hello"
cv2.namedWindow("show",0)
print "hello1"
# Load an color image in grayscale
img = cv2.imread('image.jpg',0)

print "hellp"
cv2.imshow("show",img)
cv2.waitKey(100)

cv2.destroyAllWindows()

我正在使用 python 2 IDLE。当我运行程序时,只打印 hello 。也不会创建“显示”窗口。我已经尝试过其他答案,例如“添加 waitKey() 或创建窗口。但是在我的情况下都没有用

我对 Raspberry 和 python 很陌生。我可以知道上面的代码有什么问题吗?还有为什么不打印“hello1”?

**编辑*

由于 imshow 方法不起作用,我尝试了 matplotlib。但是,现在窗口框架没有更新

import numpy as np
import cv2
from matplotlib import pyplot as plt

cap = cv2.VideoCapture(0)

while(True):
# Capture frame-by-frame
ret, frame = cap.read()
print "new frame"
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#cv2.imwrite("framenew.jpg",frame)

# Display the resulting frame
plt.imshow(gray,cmap ='gray')
plt.show()

if cv2.waitKey(0) & 0xFF == ord('q'):
break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

请问如何正确使用这个库

最佳答案

您可以将按键等待时间设置为 0 秒,如 waitKey(0)。您的代码 waitKey(100) 指示 OpenCV 等待 100 毫秒,然后销毁窗口。如果您将 waitKey(5000) 设置为 5 秒,它将显示图像 5 秒并销毁它。

下面是相关的OpenCV Doc .

The function waitKey waits for a key event infinitely (when delay <= 0 ) or for delay milliseconds, when it is positive. Since the OS has a minimum time between switching threads, the function will not wait exactly delay ms, it will wait at least delay ms, depending on what else is running on your computer at that time. It returns the code of the pressed key or -1 if no key was pressed before the specified time had elapsed.

下面是 waitKey() 的示例用法,它将永远等待 Q 在销毁 imshow() 窗口之前被按下。

if cv2.waitKey(0) & 0xFF == ord('q'):
break

希望这对您有所帮助。

关于python - Opencv imshow 不创建窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44606599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com