-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
36 lines (26 loc) · 1.16 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
from collections import Counter
# Scale function
def scale_image(image:np.ndarray, a_min=0, a_max=255):
# Create a new image with the same shape as the original image
resultImage = np.zeros(image.shape)
# Scale the image
image = image - image.min()
if image.max() == 0 and image.min() == 0:
resultImage = a_max * (image / 1)
else:
resultImage = a_max * (image / image.max())
return resultImage
def find_most_frequent_pixels(image, top_n=10):
image = np.array(image)
# Flatten the image
flattened_image = image.flatten()
# Flatten the image into a 1D array
pixel_values = image.flatten()
# Count the frequency of each pixel intensity using Counter
pixel_counts = Counter(pixel_values)
# Sort the pixel intensity counts in descending order
sorted_pixel_counts = sorted(pixel_counts.items(), key=lambda x: x[1], reverse=True)
# Get the top N frequently occurring pixel intensities
top_pixel_intensities = [pixel_intensity for pixel_intensity, count in sorted_pixel_counts[:top_n]]
return top_pixel_intensities