如何从特定视频帧中读取像素

How to read pixels from a specific video frame

我正在尝试使用 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

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

根据您的问题,您正在尝试使用 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