Search code examples
pythonopencvvideo-processing

How to find value of each pixel in all the frames of a video using python


I have a video with dimensions say 1280, 720 and 166 frames. Now I want to track the value of each pixel at a given position. Like at position (100, 100), I want to get the value of the pixel at this position in all the 166 frames. This will give me 166 value.

Likewise I want to find values of pixels at all the positions in the frame.Then fit a curve for all the values of each pixel one by one afterward.

This is code I wrote but this is able to obtain pixel values at the specified position only.

cap= cv2.VideoCapture('video.mp4')
success, image = cap.read()
count = 0
while success:
  count += 1
  v = image[500, 700]
  s = sum(v)/len(v)
  print(" Count {}  Pixel at (500, 700) - Value {} ".format(count,v))
  success, image = cap.read()
cap.release()
cv2.destroyAllWindows()

And I also tried using width and height of the frame but it is reading the positions randomly and I want them to be stored in sequence because I want to plot a graph for each of them:

while success:
  count += 1
  for x in range(0,width):
    for y in range(0,height):
      print(image[x,y,z])

So what changes do I have to make in this code to get all the values.


Solution

  • cap= cv2.VideoCapture('video.mp4')
    success, image = cap.read()
    count = 0
    while success:
      count += 1
      print("{}th image from the movie ".format(count))
      for x in range(0,1280-1):
          for y in range(0,720-1):
              v = image[y, x]
              print("Pixel at ({}, {}) - Value {} ".format(x,y,v))
      success, image = cap.read()
    cap.release()
    cv2.destroyAllWindows()