I am making my first real raid in Python and NumPy to handle some images. I have an image loaded as a 3-dimensional NumPy array, where the 0 axis represents the stripes of the image, while the 1 and 2 axes represent the columns and rows of pixels. From this, I have to take the 3x1 matrix representing each pixel and perform several operations that will lead to another 3x1 matrix, which will be used to construct the image of the results.
My first approach (simplified and with random data) is as follows:
import numpy as np import random factor = np.random.rand(3,3) input = np.random.rand(3,100,100) results = np.zeros((3,100,100)) for x in range(100): for y in range(100): results[:,x,y] = np.dot(factor,input[:,x,y])
But it seems to me inefficient and inefficient. Is there a way to do this in elementary fasion, for example:
results = np.dot(factor,input,ElementWiseOnAxis0)
In an attempt to find a solution to this problem, I came across this question, which is obviously very similar. However, the author could not solve the problem until they were satisfied. I hope that something has changed since 2012, or my problem is very different from them in order to make it more easily resolved.
python arrays numpy image-processing matrix-multiplication
Joe
source share