Skip to content

Commit

Permalink
Merge pull request #434 from Living-with-machines/426-calc-pixel-stats
Browse files Browse the repository at this point in the history
Update pixel stats calculation
  • Loading branch information
rwood-97 authored May 15, 2024
2 parents ed2af0b + 0a69fae commit 282b873
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mapreader/load/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,30 +1392,31 @@ def calc_pixel_stats(
min_x, min_y, max_x, max_y = self.patches[patch]["pixel_bounds"]
if width != max_x - min_x:
width = max_x - min_x
img = img.crop((0, 0, width, height))
if height != max_y - min_y:
height = max_y - min_y
img = img.crop((0, 0, width, height))

bands = img.getbands()

if calc_mean:
if all(f"mean_pixel_{band}" in patch_keys for band in bands):
if "mean_pixel" in patch_keys:
calc_mean = False
if calc_std:
if all(f"std_pixel_{band}" in patch_keys for band in bands):
if "std_pixel" in patch_keys:
calc_std = False

img_stat = ImageStat.Stat(img)

if calc_mean:
img_mean = img_stat.mean
self.patches[patch]["mean_pixel"] = np.mean(img_mean) / 255
for i, band in enumerate(bands):
# Calculate mean pixel values
self.patches[patch][f"mean_pixel_{band}"] = img_mean[i] / 255

if calc_std:
img_std = img_stat.stddev
self.patches[patch]["std_pixel"] = np.mean(img_std) / 255
for i, band in enumerate(bands):
# Calculate std pixel values
self.patches[patch][f"std_pixel_{band}"] = img_std[i] / 255
Expand Down

0 comments on commit 282b873

Please sign in to comment.