【问题标题】:display cv2.VideoCapture image inside Pygame surface在 Pygame 表面内显示 cv2.VideoCapture 图像
【发布时间】:2013-10-08 06:15:15
【问题描述】:

我正在尝试使用 opencv (cv2) 将网络摄像头馈送流式传输到 pygame 表面对象。问题是颜色显示不正确。我认为这是类型转换,但我无法理解 pygame 表面文档以了解它的预期。

这段代码演示了我在说什么

import pygame
from pygame.locals import *
import cv2
import numpy

color=False#True#False
camera_index = 0
camera=cv2.VideoCapture(camera_index)
camera.set(3,640)
camera.set(4,480)

#This shows an image the way it should be
cv2.namedWindow("w1",cv2.CV_WINDOW_AUTOSIZE)
retval,frame=camera.read()
if not color:
    frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.flip(frame,1,frame)#mirror the image
cv2.imshow("w1",frame)

#This shows an image weirdly...
screen_width, screen_height = 640, 480
screen=pygame.display.set_mode((screen_width,screen_height))

def getCamFrame(color,camera):
    retval,frame=camera.read()
    if not color:
        frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    frame=numpy.rot90(frame)
    frame=pygame.surfarray.make_surface(frame) #I think the color error lies in this line?
    return frame

def blitCamFrame(frame,screen):
    screen.blit(frame,(0,0))
    return screen

screen.fill(0) #set pygame screen to black
frame=getCamFrame(color,camera)
screen=blitCamFrame(frame,screen)
pygame.display.flip()

running=True
while running:
    for event in pygame.event.get(): #process events since last loop cycle
        if event.type == KEYDOWN:
            running=False
pygame.quit()
cv2.destroyAllWindows()

我的最终目标是为明年的 DIY 婚礼创建一个小型照相亭应用程序。我是编程新手,但我设法将它拼凑在一起。我还尝试使用 VideoCapture 来完成此操作,它输出一个 PIL,我也无法使用表面对象。我想使用 pygame 表面,这样我就可以制作动画并覆盖倒计时文本、边框等。

更新:问题在于 cv2 函数 camera.read() 返回 BGR 图像,但 pygame.surfarray 需要 RGB 图像。这是用线固定的

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

此外,当转换为灰度时,以下代码有效:

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

所以,getCamFrame 函数现在应该是

def getCamFrame(color,camera):
    retval,frame=camera.read()
    frame=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
    if not color:
        frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        frame=cv2.cvtColor(frame,cv2.COLOR_GRAY2RGB)
    frame=numpy.rot90(frame)
    frame=pygame.surfarray.make_surface(frame)
return frame

【问题讨论】:

  • 我打算进来回答你的问题,因为它被列在未回答的问题上。最好发布并接受您的解决方案作为自我回答,而不是将其包含在问题中。
  • 你应该接受我的回答,因为这解决了你的颜色问题。并且不使用解决方案更新您的问题。

标签: python opencv pygame webcam


【解决方案1】:

这里没有颜色错误

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

所以对于正常的屏幕颜色,您只需将其更改为

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

因为它对我有用,所以会这样做

【讨论】:

    【解决方案2】:

    我试过你的代码,但我只得到一张照片而不是电影,所以我复制了

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

    进入一个while循环,它工作但随后视频被翻转,为了修复它我添加了cv2.flip(frame,1,frame) # mirror the imageframe=numpy.rot90(frame)getcamFrame 函数中,现在一切正常。

    对不起英语不好。

    【讨论】:

      【解决方案3】:

      这对我有用

      在您的代码中调整它...

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-20
        • 1970-01-01
        • 2017-07-10
        • 1970-01-01
        • 1970-01-01
        • 2019-10-05
        • 1970-01-01
        相关资源
        最近更新 更多