Skip to content Skip to sidebar Skip to footer

Display Cv2.videocapture Image Inside Pygame Surface

I'm trying to use opencv (cv2) to stream a webcam feed into a pygame surface object. The problem is the colors aren't displaying correctly. I think it is the type casting, but I'm

Solution 1:

No the color error lies in here

frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

so for normal screen color u simply change that to

frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)

That will do because it works for me

Solution 2:

I tried your code, but I'm only getting a picture not a movie, so i copied

frame=getCamFrame(color,camera)
screen=blitCamFrame(frame,screen)
pygame.display.flip()

into a while loop, it worked but then the video was flipped, to fix it i added cv2.flip(frame,1,frame) # mirror the image before frame=numpy.rot90(frame) in getcamFrame function and now everything works fine.

Sorry for the poor English.

Solution 3:

This worked for me

to adapt it in your code ...

windowSurface = screen  #or use directly variable screen

    # convert windowSurface to cv2 ------------------------------------view= pygame.surfarray.array3d(windowSurface)
    #  convertfrom (width, height, channel) to (height, width, channel)
    view= view.transpose([1, 0, 2])
    #  convertfrom rgb to bgr
    img_bgr = cv2.cvtColor(view, cv2.COLOR_RGB2BGR)
    cv2.imshow('windowname',img_bgr)
    cv2.waitKey(10)

Post a Comment for "Display Cv2.videocapture Image Inside Pygame Surface"