[英]Is there an efficient way of extracting individual pixels from a number of frames?
我对使用 Python 和 OpenCV 非常陌生,并且真的很难想出解决这个问题的方法。
我有一个长度为 2500 的视频/图像序列,即视频的持续时间为 5 秒,采样频率为 500 Hz。 我试图提取尺寸为 150x400 的感兴趣区域的像素值,因此我应该有 60'000 个长度为 2500 的单个像素。
我已经加载了我的视频,我正在尝试遍历帧以沿时域提取每个像素实例并存储它,以便我以后可以处理它。
有没有人知道如何执行这个?
我可以为我所做的事情提供代码,但到目前为止我还没有取得任何成功。
非常欢迎所有帮助和建议!
谢谢!
这是一些读取视频的代码,从每一帧中获取 ROI,并将其存储在一个大数组中。
import numpy as np
import cv2 as cv
cap = cv.VideoCapture("somevideo.mkv")
assert cap.isOpened(), "video can't be opened... is the path correct?"
nframes = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
assert nframes == 2500, f"you said 2500 frames but it's {nframes} frames"
roi_x, roi_y = 12, 34 # top left
roi_width, roi_height = 400, 150
cube = np.empty((nframes, roi_height, roi_width, 3), dtype=np.uint8)
# 3-channel: assuming it's RGB/BGR data, not gray
for i in range(nframes):
rv, frame = cap.read()
assert rv, "video ended before it said it would!"
cube[i] = frame[roi_y:roi_y+roi_height, roi_x:roi_x+roi_width]
cap.release()
谢谢大家的回复。 我今天对此进行了研究,并认为我对我的解决方案感到满意。 我显然没有导入视频文件,而是从笔记本电脑上的文件夹中取出保存的帧,然后在时域中提取单个像素。 虽然,接下来我将实现您的视频代码 @Christopher 并清理我的。 谢谢!
import numpy as np
import cv2
import glob
### Reading in all of the chessboard files ###
cv_img = []
for img in glob.glob("/Python/OpenCv/Images/chessboard/*.jpg"):
n = cv.imread(img)
cv_img.append(n)
list1 = np.empty((len(cv_img),100,100), dtype=np.uint8)
i = 0
for img in cv_img:
img = img[100:200,100:200]
new_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
list1[i] = new_img
i = i+1
img = img + 1
column = 0;
pixels_clear = np.ones((1,13)) #no_of_frames, one column 5,1
pixels = np.ones((1,13))
for rows in range(0,100):
column=0
while column < 100:
n_frame = 0
for n_frame in range(0,13):
pixels_clear[0,n_frame] =list1[n_frame,rows,column]
n_frames = n_frames + 1
column = column + 1
pixels = np.vstack([pixels,pixels_clear])
"""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.