Skip to content
Advertisement

How can you store values in a new matrix without indexing it?

New to python and also using stack overflow so please bear with me, I’m just trying to get used to it.

Right, so I have 5 image frames. I wish to extract the pixel values from these frames in the time domain. Therefore I need to store the values in a new matrix with no_of_frames as my rows (time domain ) and pixel instance ( 9 * 9 image so 81 instances ) as my columns.

I can do that as shown below ( I know the code is awful, bear with me) but when it comes to storing the pixel values for the next row I run into an issue as I have used the while loop up to 400 columns.I want to continue to store my values as I did for the first row.

  cap = cv2.VideoCapture(0)
  cv_img = []

    while(True):

       ret, frame1 = cap.read()
       time.sleep(1/25)
       ret, frame2 = cap.read()
       time.sleep(1/25)
       ret, frame3 = cap.read()
       time.sleep(1/25)
       ret, frame4 = cap.read()
       time.sleep(1/25)
       ret, frame5 = cap.read()

      if cv2.waitKey(1000) & 0xFF == ord('q'):
          break


     cap.release()
     cv2.destroyAllWindows()

     ### Region of Interest ###
     frame_slice1 = frame1[300:400,400:800]
     frame_slice2 = frame2[300:400,400:800]
     frame_slice3 = frame3[300:400,400:800]
     frame_slice4 = frame4[300:400,400:800]
     frame_slice5 = frame5[300:400,400:800]

     ### Converting the ROI into Grayscale ###
     gray1 = cv2.cvtColor(frame_slice1, cv2.COLOR_BGR2GRAY)
     cv_img.append(gray1)
     gray2 = cv2.cvtColor(frame_slice2, cv2.COLOR_BGR2GRAY)
     cv_img.append(gray2)
     gray3 = cv2.cvtColor(frame_slice3, cv2.COLOR_BGR2GRAY)
     cv_img.append(gray3)
     gray4 = cv2.cvtColor(frame_slice4, cv2.COLOR_BGR2GRAY)
     cv_img.append(gray4)
     gray5 = cv2.cvtColor(frame_slice5, cv2.COLOR_BGR2GRAY)
     cv_img.append(gray5)



     column = 0;
     pixels_clear = np.ones((5,1)) #no_of_frames, one column 5,1
     pixels = np.ones((5,40000)) #no_of_frames, no of pixel elements 5,40000

     for rows in range(0,100):

          while column < 400: 
        
        j=0
        pixels_clear[j,0] = gray1[rows,column]
        j = j+1
        pixels_clear[j,0] = gray2[rows,column]
        j = j+1
        pixels_clear[j,0] = gray3[rows,column]
        j = j+1
        pixels_clear[j,0] = gray4[rows,column]
        j = j+1
        pixels_clear[j,0] = gray5[rows,column]
       
        pixels[:,column:column+1] = pixels_clear
        column = column + 1 
        

Any and all tips and suggestions are welcome. If I havent been clear (which I’m sure I have messed up on), let me know and I will try rephrase.

Thank you, P

Advertisement

Answer

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])
        
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement