Finding the minimum and maximum pixel values

I calculated the smallest and largest pixel values ​​for a pixel in a grayscale image as follows:

smallest = numpy.amin(image)
biggest = numpy.amax(image)

but it will only work in shades of gray.

How can I do the same for color image (RGB)?

+8
source share
5 answers

You can access each channel using fragments as follows:

# red
image[..., 0].min()
image[..., 0].max()
# green
image[..., 1].min()
image[..., 1].max()
# blue
image[..., 2].min()
image[..., 2].max()
+7
source

You can quickly test it in a python script.

import numpy
img = numpy.zeros((10,10,3), dtype=numpy.int)  # Black RGB image (10x10)
img[5,2] = [255, 255, 255]
print img.reshape((img.shape[0]*img.shape[1], 3)).max(axis=0)

array ([255, 255, 255])

+3
source

, BGR ( OpenCV), :

import numpy as np

max_channels = np.amax([np.amax(img[:,:,0]), np.amax(img[:,:,1]), np.amax(img[:,:,2])])

print(max_channels)
+1
import numpy
img = numpy.zeros((10,10,3), dtype=numpy.int)  
img[5,2] = [255, 255, 255]
print img.reshape((img.shape[0]*img.shape[1], 3)).max(axis=0)
0

, :

smallest = np.amin(image, axis=(0, 1))
largest = np.amax(image, axis=(0, 1))

:

smallest = image.min(axis=0).min(axis=0)
biggest = image.max(axis=0).max(axis=0)

If you want to get the results as lists, just add .tolist()at the end of each of the above.

0
source

All Articles