[PYTHON] Consider the speed of processing to shift the image buffer with numpy.ndarray

background

When processing a video, you may want to buffer n consecutive frames. So, when you want to roll the buffer and use it, you have to shift the index of the buffer.

When processing with numpy, I think there are the following two processing methods.

  1. Use the numpy.roll method
  2. Shift the index of array and assign

I want to take the fastest processing method possible, so I will consider which of these two processes is faster.

environment

inspection result

I tried the following process on jupyter notebook.

Make the first index of array the index indicating the number of images

arr1 = np.zeros((30, 720, 1280))
# numpy.use the roll method
%timeit np.roll(arr1, 1, axis = 0)
# 147 ms ± 1.73 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

#Substitute by shifting by index
%timeit arr1[1:,:,:] = arr1[0:-1,:,:]
# 189 ms ± 7.14 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Set the last index of array as the index indicating the number of images

arr2 = np.zeros((720, 1280, 30))
# numpy.use the roll method
%timeit np.roll(arr2, 1, axis = 2)
# 186 ms ± 2.71 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

#Substitute by shifting by index
%timeit arr2[:,:, 1:] = arr2[:,:,0:-1]
# 203 ms ± 1.06 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

It seems that using numpy.roll is faster under the condition that the image size is large. Also, it seems faster to switch images at the first index.

If the image is small

When the image is very small (20x20pixel), the result is that the index shift is faster as shown below.

arr3 = np.zeros((30, 20, 20))
# numpy.use the roll method
%timeit np.roll(arr3, 1, axis = 0)
# 17.1 µs ± 551 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

#Substitute by shifting by index
%timeit arr3[1:,:,:] = arr3[0:-1,:,:]
# 7.36 µs ± 90.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Points I was addicted to in terms of processing implementation

#mistake
arr[0:-2,:,:] = arr[1:-1,:,:]
arr[-1,:,:] = new_image

#Correct answer
arr[0:-1,:,:] = arr[1:,:,:]
arr[-1,:,:] = new_image

Setting the first index to -1 specifies the last index.

arr[1:-1,:,:]Then


 At first glance, it seems that you can specify from the second to the first (up to the last index).
 Actually, since the second to the penultimate is specified, the movement is that the last image does not shift to the front.



Recommended Posts

Consider the speed of processing to shift the image buffer with numpy.ndarray
I tried to compare the processing speed with dplyr of R and pandas of Python
How to increase the processing speed of vertex position acquisition
Drawing with Matrix-Reinventor of Python Image Processing-
I tried to get the batting results of Hachinai using image processing
How to crop the lower right part of the image with Python OpenCV
Try to image the elevation data of the Geographical Survey Institute with Python
[Image recognition] How to read the result of automatic annotation with VoTT
Crop the image to rounded corners with pythonista
About the processing speed of SVM (SVC) of scikit-learn
I tried to correct the keystone of the image
Image processing? The story of starting Python for
I checked the processing speed of numpy one-dimensionalization
Image processing with MyHDL
Extract the table of image files with OneDrive & Python
Add information to the bottom of the figure with Matplotlib
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 6] Introduction to scikit-learn with 100 knocks of language processing
Convert the image in .zip to PDF with Python
Try to get the contents of Word with Golang
[Chapter 3] Introduction to Python with 100 knocks of language processing
Image processing with Python
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
Image Processing with PIL
Return the image data with Flask of Python and draw it to the canvas element of HTML
I tried "gamma correction" of the image with Python + OpenCV
I tried to find the average of the sequence with TensorFlow
Wrap C/C ++ with SWIG to speed up Python processing. [Overview]
Database search (verification of processing speed with or without index)
Image crawling summary performed at the speed of a second
Settings to debug the contents of the library with VS Code
100 language processing knock-29: Get the URL of the national flag image
Try to automate the operation of network devices with Python
Attempt to automatically adjust the speed of time-lapse movies (Part 2)
[Let's play with Python] Image processing to monochrome and dots
The image display function of iTerm is convenient for image processing.
Get the source of the page to load infinitely with python.
Try to extract the features of the sensor data with CNN
Image processing with Python (Part 2)
opencv-python Introduction to image processing
python3 Measure the processing speed.
Image processing with PIL (Pillow)
Dot according to the image
Parallel processing with Parallel of scikit-learn
Image processing with Python (Part 1)
Image processing with Python (Part 3)
[Python] Image processing with scikit-image
What to do when a part of the background image becomes transparent when the transparent image is combined with Pillow
I compared the speed of Hash with Topaz, Ruby and Python
Understand the function of convolution using image processing as an example
The story of not being able to run pygame with pycharm
Save the results of crawling with Scrapy to the Google Data Store
Speed comparison of Wiktionary full text processing with F # and Python
Become familiar with (want to be) around the pipeline of spaCy
I tried to automate the watering of the planter with Raspberry Pi
[Statistics] Grasp the image of the central limit theorem with a graph
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
Display the image of the camera connected to the personal computer on the GUI.
[Python] Try to graph from the image of Ring Fit [OCR]
I tried to build the SD boot image of LicheePi Nano