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

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

我正在尝试使用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个回答
0
投票

根据您的问题,您正在尝试使用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
© www.soinside.com 2019 - 2024. All rights reserved.