简体   繁体   中英

Is there an efficient way of extracting individual pixels from a number of frames?

I am very new to using Python and OpenCV and really struggling to come up with a solution to this problem.

I have a video/image sequences of length 2500 ie the duration of the video is 5 seconds with a sampling frequency of 500 Hz. I am trying to extract the pixel values of a region of interest that has dimensions of 150x400, therefore I should have 60'000 individual pixels of length 2500.

I have loaded in my video and I am trying to loop through the frames to extract each pixel instance along the time domain and store this so I can process it later.

Does anyone have any idea on how to execute this?

I can provide code for what I have done, but I havent had any success with it so far.

All help and suggestions are very much welcome!

Thank you!

here's some code that reads a video, takes a ROI from each frame, and stores that in one big array.

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()

Thank you all for replying. I worked on this today and think I am happy with my solution. I obviously didnt import a video file but just took the saved frames from a folder on my laptop and then extracted the individual pixels in the time domain. Although, next I will implement your video code @Christopher and clean mine up. Thank you!

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])

"""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM