簡體   English   中英

有沒有一種從多個幀中提取單個像素的有效方法?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM