-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageProcessing.py
131 lines (101 loc) · 4.27 KB
/
imageProcessing.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
import matplotlib.image as mpimg
import os
def create_channel(points, shape, radius=2):
"""
Creates an image channel of the given shape where circles of a given radius
around point1 and point2 are set to 255, and the rest are zeros.
Args:
list of points (x, y) or [x, y]
shape (tuple): Shape of the output channel (height, width).
radius (int): Radius of the circles to draw around the points.
Returns:
numpy.ndarray: A 2D array representing the channel.
"""
# Create a blank channel
channel = np.zeros(shape, dtype=np.uint8)
# Helper function to draw a circle around a point
def draw_circle(center, radius):
cx, cy = center
y, x = np.ogrid[: shape[0], : shape[1]] # Create grid of coordinates
distance = (x - cx) ** 2 + (
y - cy
) ** 2 # Compute squared distances from center
mask = distance <= radius**2 # Define the circular mask
channel[mask] = 255 # Apply mask to set values to 255
# Draw circles around the provided points
for point in points:
draw_circle(point, radius)
return channel
"""
take single-channel image img (numpy array) and add red and green channels highlighting the 2 points
(result has 3-channels)
"""
def add_point_channels(img, point1, point2, radius=2):
# Create a channel to visualise the points
shape = img.shape
x1, y1, x2, y2 = point1[0], point1[1], point2[0], point2[1]
point1_channel = create_channel([(x1, y1)], shape, radius=radius)
point2_channel = create_channel([(x2, y2)], shape, radius=radius)
# Turn one-channel simplified image to 3 channels
rgb = np.stack([img] * 3, axis=-1)
# Integrate our channel that represents the points into the image
red_channel = rgb[:, :, 0]
red_channel = np.maximum(red_channel, point1_channel)
rgb[:, :, 0] = red_channel
green_channel = rgb[:, :, 1]
green_channel = np.maximum(green_channel, point2_channel)
rgb[:, :, 1] = green_channel
return rgb
def add_point_channels_multiplepoints(img, points1, points2, radius=2):
# Create a channel to visualise the points
shape = img.shape
point1_channel = create_channel(
[(point[0], point[1]) for point in points1], shape, radius=radius
)
point2_channel = create_channel(
[(point[0], point[1]) for point in points2], shape, radius=radius
)
# Turn one-channel simplified image to 3 channels
rgb = np.stack([img] * 3, axis=-1)
# Integrate our channel that represents the points into the image
red_channel = rgb[:, :, 0]
red_channel = np.maximum(red_channel, point1_channel)
rgb[:, :, 0] = red_channel
green_channel = rgb[:, :, 1]
green_channel = np.maximum(green_channel, point2_channel)
rgb[:, :, 1] = green_channel
return rgb
def load_images_from_folder(rel_path_source, max_images=400):
"""
Load images from a folder using Matplotlib and NumPy.
Args:
rel_path_source (str): The relative path to the folder containing the images.
max_images (int): Maximum number of images to load. Defaults to 400.
Returns:
tuple: A tuple containing:
- images (list): A list of NumPy arrays representing the images.
- image_names (list): A list of corresponding image file names.
"""
images = []
image_names = []
abs_path = os.path.abspath(
rel_path_source
) # Resolve the relative path to an absolute path
if not os.path.exists(abs_path):
raise ValueError(f"Path '{rel_path_source}' does not exist.")
# Iterate through all files in the directory
for idx, file_name in enumerate(os.listdir(abs_path)):
if idx >= max_images: # Stop if the maximum number of images is reached
break
file_path = os.path.join(abs_path, file_name)
# Try to load the file as an image
try:
img = mpimg.imread(file_path) # Load the image as a NumPy array
if img is not None:
images.append(img)
image_names.append(file_name)
except (IOError, ValueError):
# Skip files that are not images or cannot be opened
print(f"Skipping non-image file or unreadable file: {file_name}")
return images, image_names