【问题标题】:How to read pixels from a specific video frame如何从特定视频帧中读取像素
【发布时间】:2019-04-24 10:06:58
【问题描述】:

我正在尝试使用 Python 中的 OpenCV 更改特定视频帧中的像素。 我当前的代码是:

import cv2
cap = cv2.VideoCapture("plane.avi")
cap.set(1, 2) #2- the second frame of my video
res, frame = cap.read()
cv2.imshow("video", frame)
while True:
    ch = 0xFF & cv2.waitKey(1)
    if ch == 27:
        break

我得到了我想要的框架,但我不知道如何获取和更改它的像素。 请提出一种方法。

【问题讨论】:

    标签: python video frame pixel cv2


    【解决方案1】:

    根据您的问题,您正在尝试使用 cv2.seek() 读取第二帧。像素值存储在变量帧中。为了更改它,您可以访问单个像素值。

    例子:

    cap.set(1, 2)
    res, frame = cap.read() #frame has your pixel values
    
    #Get frame height and width to access pixels
    height, width, channels = frame.shape
    
    #Accessing BGR pixel values    
    for x in range(0, width) :
         for y in range(0, height) :
              print (frame[x,y,0]) #B Channel Value
              print (frame[x,y,1]) #G Channel Value
              print (frame[x,y,2]) #R Channel Value
    

    【讨论】:

    • 非常感谢,我现在知道了!如果我像这样更改此代码中的 B、G、R 通道:frame[1,1,0] = 255 frame[1,1,1] = 255 frame[1,1,2] = 255 我将像素设为白色,是吗?
    • 是的。确切地。很高兴你想出来了。你能接受我的回答并结束话题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 1970-01-01
    • 2013-04-16
    相关资源
    最近更新 更多