cv2.cvtColor(img,cv2.COLOR_BGR2RGB)不起作用

问题描述 投票:-1回答:1

我正在尝试使用python中的mss和Opencv创建一个屏幕录像机,我捕获的视频与原始计算机屏幕的颜色非常不同。我试图在线找到解决方案,每个人都说应该使用cvtColor()进行修复,但是我的代码中已经包含了它。

import cv2
from PIL import Image
import numpy as np
from mss import mss
import threading
from datetime import datetime

`

def thread_recording():

    fourcc=cv2.VideoWriter_fourcc(*'mp4v')
    #fourcc=cv2.VideoWriter_fourcc(*'XVID')
    out=cv2.VideoWriter(vid_file,fourcc,50,(width,height))
    mon = {"top": 0, "left": 0, "width":width, "height":height}
    sct = mss.mss()

    thread1=threading.Thread(target=record,args=(mon,out,sct))
    thread1.start()

def record(mon,out,sct):

    global recording
    recording=True

    while recording:
        frame= np.asarray(sct.grab(mon))
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        out.write(frame)

    out.release()

vid_file变量包含带有mp4扩展名的输出文件名的字符串

Screenshot of my screen

Screenshot from recorded video

python-3.x opencv mss
1个回答
0
投票

[因此,我环顾了一下,发现这显然是病房中3.x版本的opencv中的一个错误。然后,我尝试使用PIL获取RGB图像并删除了cvtColor(),但它产生了一个空视频。因此,我放回去并繁荣发展。即使cvtColor()由于某种未知原因似乎无所事事,它也必须存在。当您将PIL Image与cvtColor()一起使用时,它会按预期方式写入视频]]

from PIL import Image
def record(mon,out,sct):

    global recording
    recording=True

    while recording:
        frame=sct.grab(mon)
        frame = Image.frombytes('RGB', frame.size, frame.rgb)
        frame = cv2.cvtColor(np.array(frame), cv2.COLOR_BGR2RGB)
        out.write(np.array(frame))

    out.release()

由于我是编程新手,如果我错过或忽略了一些重要的事情,我将非常感谢您的帮助

© www.soinside.com 2019 - 2024. All rights reserved.