Update helpers

This commit is contained in:
Andras Schmelczer 2024-04-08 08:02:31 +01:00
parent c0b0dacd99
commit f5c03db198
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 140 additions and 4 deletions

View file

@ -2,13 +2,17 @@ from PIL import Image
import numpy as np
def compute_histogram(image_path, bins: int, value_range=(0, 256)):
def compute_histogram(
image_path, bins: int, value_range=(0, 256), normalize: bool = True
):
image = Image.open(image_path)
image = np.array(image)
histogram, _ = np.histogramdd(
image.reshape(-1, 3), bins=bins, range=[value_range, value_range, value_range]
)
histogram = histogram / np.sum(histogram)
).astype(np.float64)
return histogram.astype(np.float32)
if normalize:
histogram = histogram / np.sum(histogram)
return histogram