我正在使用新的 PySDL2 包,试图将它与我现有的 OpenCV 代码接口。我想通过 cv2 python 接口从网络摄像头捕捉到 OpenCV 的图像,并使用 PySDL2 在屏幕上的窗口中显示它。我想我想出了如何正确地将 cv2 图像格式转换为 PySDL2 表面,但是在下面的代码末尾,我得到的只是一个黑色窗口。任何关于我出错的地方的指示将不胜感激!
#grab a frame from a webcam
import cv2
vc = cv2.VideoCapture(0)
junk,image = vc.read()
#convert image to sdl format (?)
import sdl2
sbuf = image.tostring()
simage = sdl2.SDL_CreateRGBSurfaceFrom(sbuf,image.shape[0],image.shape[1],24,3*image.shape[0],sdl2.SDL_PIXELFORMAT_BGRA8888,0xff0000, 0x00ff00, 0x0000ff, 0)
#create a window
sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO)
windowSize = (640,480)
window = sdl2.SDL_CreateWindow(b"Hello World",sdl2.SDL_WINDOWPOS_CENTERED, sdl2.SDL_WINDOWPOS_CENTERED,windowSize[0], windowSize[1], sdl2.SDL_WINDOW_SHOWN)
windowSurface = sdl2.SDL_GetWindowSurface(window)
#try to blit the sdl-formatted image to the window
sdl2.SDL_BlitSurface(simage,None,windowSurface,None)
sdl2.SDL_UpdateWindowSurface(window)
sdl2.SDL_FreeSurface(simage)
# pump events to get the window to show and update
while True:
sdl2.SDL_PumpEvents()