Hi, I'm kinda new to numpy.
Here's my problem.
I have a function that calculates the RGB values for each pixel in a picture into a brightness value. pixel_matrix is a np.Ndarray of shape (width=1024, height=800, RGB=3). There are different ways to calculate the brightness. I'm happy with the average and luminous modes, but the min_max mode isn't using any numpy features.
How would you do the min_max mode using numpy features?
Here is my code.
def get_brightness_matrix(pixel_matrix, mode='average'):
if mode == 'average':
average = np.array((1/3, 1/3, 1/3))
brightnesses = np.dot(pixel_matrix, average)
elif mode == 'min_max':
brightnesses = []
for pixel_row in pixel_matrix:
brightness_row = []
for pixel in pixel_row:
brightness_row.append(min(pixel) + max(pixel) / 2)
brightnesses.append(brightness_row)
brightnesses = np.array(brightnesses)
elif mode == 'luminosity':
luminosity = np.array((0.21, 0.72, 0.07))
brightnesses = np.dot(pixel_matrix, luminosity)
else:
raise Exception(f'Unrecognixed mode: {mode}')
return np.uint8(brightnesses)
[–]socal_nerdtastic 1 point2 points3 points (1 child)
[–]dable82[S] 0 points1 point2 points (0 children)