我要显示图像-例如使用网络摄像头捕获-散景。我尝试了image_url和image_rgba,但是都无法正常工作。 image_url没有显示任何内容,image_rgb显示了一些内容,但是似乎有一些索引shif。
# -*- coding: utf-8 -*-
from bokeh.plotting import figure, show
import scipy.misc
import cv2
import matplotlib.pyplot as plt
import os
# capture video frame from webcam
#vc = cv2.VideoCapture(1)
vc = cv2.VideoCapture(-0)
rval, frame = vc.read()
vc.release()
# show captured image (figure 1)
fig = plt.figure()
ax = plt.Axes(fig,[0,0,1,1])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(frame)
plt.show()
# save captured image
scipy.misc.imsave('outfile.jpg', frame)
mypath = os.path.join(os.getcwd(),'outfile.jpg')
# 1. try using image_url ... not working (figure 2)
p = figure()
p.image_url(url=[mypath],x=[0],y=[1],w=[10],h=[10])
show(p)
# 2. try using image_rgba ... also not working (figure 3)
p = figure(x_range=(0,10), y_range=(0,10))
p.image_rgba(image=[frame], x=0, y=0, dw=10, dh=10)
show(p)
# 3. solution provided by @bigreddot ... flipped but also not working (figure4)
img = scipy.misc.imread(mypath)[::-1] # flip y-direction for Bokeh
p = figure(x_range=(0,10), y_range=(0,10))
p.image_rgba(image=[img], x=0, y=0, dw=10, dh=10)
show(p)
更新: :根据@bigreddot的评论
图1
我使用的bokeh版本是0.13,python版本3.6.0
答案 0 :(得分:2)
经过调查,OpenCV的返回结果是一个Numpy字节数组,其形状为(M,N,3),即RGB tuples 。 Bokeh期望的是一个Numpy数组,其形状为(M,N)的32位整数,表示RGBA值。因此,您需要从一种格式转换为另一种格式。这是一个完整的示例:
from bokeh.plotting import figure, show
import scipy.misc
import cv2
import os
import numpy as np
#vc = cv2.VideoCapture(1)
vc = cv2.VideoCapture(-0)
rval, frame = vc.read()
vc.release()
M, N, _ = frame.shape
img = np.empty((M, N), dtype=np.uint32)
view = img.view(dtype=np.uint8).reshape((M, N, 4))
view[:,:,0] = frame[:,:,0] # copy red channel
view[:,:,1] = frame[:,:,1] # copy blue channel
view[:,:,2] = frame[:,:,2] # copy green channel
view[:,:,3] = 255
img = img[::-1] # flip for Bokeh
p = figure()
p.image_rgba(image=[img],x=0,y=0, dw=10, dh=10)
show(p)
生成: