0

Assume we have a np.array containing set of grayscale images. How to find the mode of each pixel position? Normally numpy have np.mean() and np.median() but mode is not included. I tried out stats.mode() of scipy but its slow. I also get bit confused with axis term. Hope someone can explain that also (Eg: axis=0, axis=1 etc.)

Input:
[[ [[1,2][3,2]] [[4,3][5,3]] [[9,5][4,6]] ],
 [ [[1,3][3,3]] [[2,7][1,4]] [[4,1][1,7]] ],
 [ [[1,5][3,3]] [[4,3][1,6]] [[6,4][5,6]] ]
]

Output:
[[ [[1,2][3,3]] [[4,3][1,3]] [[4,1][1,6]] ]]

1 Answer 1

3

The given axis sets which axis to take the metric (in your case mode) over. This axis is collapsed into a single value.

I think your example array might be misformatted, so here's another example:

Let's say you have 3 800x600 grayscale images, im0, im1, and im2. Their shape, given by im0.shape, is (800, 600). We put them together like this:

ims = numpy.array([im0, im1, im2])
print(ims.shape)  # -> (3, 800, 600)

Now let's say we want the mean value for each pixel over our 3 images. Then we want to take the mean over axis 0. This axis is collapsed into the mean, leaving us with the mean image.

mean_im = numpy.mean(ims, axis=0)
print(mean_im.shape)  # -> (800, 600)

We can do the same thing with scipy.stats.mode, but note that both the mode and the count is returned, so we have to do something like this:

mode, count = stats.mode(ims, axis=0)
Sign up to request clarification or add additional context in comments.

2 Comments

what will the output of the following : mean_im = numpy.mean(ims, axis=1) print(ims.shape) ?
Then you take then mean across the x-coordinate in every image. In other words, this collapses the columns into one, or you can say that you take the mean of every row in every image. The shape will be (3, 600) - one value for every row in every image.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.