简体   繁体   中英

subtract a smaller ndarray from a larger onethen use whats left to pad the smaller ndarray to the size as the larger ndarray

I have an ideal ndarray size that represents a video as a numpy array. The size has resolution 1080X1920Xfr where Fr is the number of frames. Given a random video, it may not have the correct resolution for the excersize so we pad the frames with blue pixels till it has resolution 1080X1920.

We would like to do it in one go rather than loop through the frames and I was thinking we could

have a value of fr that's longer than a typical video.

Somehow subtract the random video from ideal video

make all the pixel values in the ideal video blue

then add the pieces subtracted of from the ideal to the random video.

so we would initially check to see if the number of frames in the random video is > Fr...if it is we skip, then proceed to do the subtraction.

I have code that takes a video, flattens it and checks to see if the size is greater than a certain value, if it is skip if it is not then it creates a numpy array with the difference, populates it with 0's and appends that to the video before reshaping it back. that makes all the videos being process have the same number of frames.

here is the code for that. I take it you see the code for the above is much more complex and your help in resolving it would be appreciated.

     videodata = skvideo.io.vread(dirname2)

     videodata = videodata.flatten()

     # following code pads the video so that it all videos are the same size
     x = (1080 * 1920 * 3 * 12600) - videodata.size
     y = [[0] for i in range(x)]
     y = np.asarray(y)
     y = y.flatten()

     videodata = np.concatenate((videodata, y), -1)
     videodata = videodata.reshape(1, 12600, 1080, 1920, 3)```

From what I understood from the question there are two subproblems to you question:

  1. Video resolution is not equal to 1080x1920.
  2. No of timesteps, ie no of video frames should be equal to a certain number (12600 in the example provided).

Both the problems can be solved with numpy.pad .

Let the videodata be array of size 1065x1900 with 1200 frames.

a = np.random.random((1065, 1900, 3, 1200))

So to make the video resolution 1080x1920 we can do the following:

b = np.pad(a, ((7,8),(10,10),(0,0),(0,0)), mode='constant', constant_values=0)
b.shape

You'd get (1080, 1920, 3, 1200) . So now your video is 1080x1920. The value (7,8) instructs numpy to pad (before, after) in that axis, so we pad 7 values in front and 8 values at the end of the first axis.

Now to add additional frames we'll pad along the last axis.

b = np.pad(a, ((7,8),(10,10),(0,0),(0,10)), mode='constant', constant_values=0)
b.shape

We'll get (1080, 1920, 3, 1200) . Thus we added 10 more frames to the video.

Hope this answers your question.

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