我的相机有一个字节串,我想在不保存的情况下使用OpenCV显示帧(节省时间)。我已经检查了这个问题:Python OpenCV从字节字符串加载图像,但出现错误failed to import cv from cv2
。
打印(帧): b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\...
我的代码(在ofc循环中):
frame = # need to convert bytestring for imshow()
cv2.imshow('image', frame)
cv2.waitKey(1)
另外,如果我只是将其保存为文件,然后使用cv2.imread(..)进行加载,则代码也可以正常工作,但是最好的方法是跳过filesave并加载。
因此,这不再起作用,因为cv已从OpenCV3中删除。
nparr = np.fromstring(frame, np.uint8)
img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img_ipl = cv.CreateImageHeader((img_np.shape[1], img_np.shape[0]), cv.IPL_DEPTH_8U, 3)
cv.SetData(img_ipl, img_np.tostring(), img_np.dtype.itemsize * 3 * img_np.shape[1])
cv2.imshow('image', img_ipl)
cv2.waitKey(1)
然后如何在不创建临时文件的情况下传递字节字符串?
因此,找到了解决方案:
# converting bytestring frame into imshow argument
nparr = np.fromstring(frame, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
cv2.imshow('image', frame)
cv2.waitKey(1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句