将cv2图像转换为PySDL2表面以进行blitting到屏幕

时间:2013-08-25 22:24:31

标签: python opencv sdl pysdl2

我正在使用新的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()

3 个答案:

答案 0 :(得分:2)

解决了!

#import necessary modules
import cv2
import sdl2
import sdl2.ext
import numpy

windowSize = (640,480)

#initialize the camera
vc = cv2.VideoCapture(0)
vc.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, windowSize[0])
vc.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, windowSize[1])

#grab and show a first frame from the camera
junk,image = vc.read()
cv2.imshow('0',image)

#initialize sdl2
sdl2.ext.init()
window = sdl2.ext.Window("test", size=windowSize)
window.show()
windowSurf = sdl2.SDL_GetWindowSurface(window.window)
windowArray = sdl2.ext.pixels3d(windowSurf.contents)

while True: #keep reading to have a live feed from the cam
    junk,image = vc.read()
    image = numpy.insert(image,3,255,axis=2) #add alpha
    image = numpy.rot90(image) #rotate dims
    numpy.copyto(windowArray, image)
    window.refresh()

我不确定为什么使用cv2.imshow从相机显示第一帧是必要的,但没有那部分,sdl2窗口永远不会出现。

答案 1 :(得分:1)

我没有足够的声誉来添加评论,尽管它已经很久了, 希望这可以帮助某人,

您可以为窗口创建添加标志

window = sdl2.ext.Window("test", size=windowSize, flag=sdl2.SDL_WINDOW_SHOWN)

答案 2 :(得分:0)

此行可能会帮助您:

windowArray [:,:,0:3] = img.swapaxes(0,1)


#!python3
import cv2
import sdl2
import sdl2.ext

def process_frame(img):
    # --- initialize
    sdl2.ext.init()
    window = sdl2.ext.Window("sdl",size=('width','height'))
    window.show()
    events = sdl2.ext.get_events()
    for event in events:
        if event.type == sdl2.SDL_QUIT:
            exit(0)
    # --- initialize
    windowArray = sdl2.ext.pixels3d(window.get_surface()) 

    # --- convert cv2 frame to sdl windowArray
    windowArray[:,:,0:3] = img.swapaxes(0,1)


vc = cv2.VideoCapture(0)     
while vc.isOpened():
    ret,frame = cap.read()
    if ret == True:
        process_frame(frame)
    else:
        break