Skip to content

Commit 4b6f9c8

Browse files
committed
polarisation function
1 parent ef3ad8a commit 4b6f9c8

11 files changed

Lines changed: 71 additions & 0 deletions

File tree

find_polarisation.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from skimage import feature, measure
4+
from typing import Tuple
5+
6+
def find_polarisation(
7+
image: np.ndarray,
8+
mask: np.ndarray,
9+
display:bool = False) -> Tuple[[Tuple[int, int]], Tuple[float, float]]:
10+
11+
"""
12+
Finds the global maximum and centroid of a mask in an image and optionally displays the results.
13+
14+
Parameters:
15+
- image (np.ndarray): The input image as a 2D NumPy array.
16+
- mask (np.ndarray): The binary mask as a 2D NumPy array.
17+
- display (bool): Flag to display the image, mask, and annotations. Default is False.
18+
19+
Returns:
20+
- Tuple[Optional[Tuple[int, int]], Tuple[float, float]]: A tuple containing:
21+
- The coordinates of the global maximum as a tuple of integers (row, column), or None if no local maxima are found.
22+
- The coordinates of the centroid of the mask as a tuple of floats (row, column).
23+
24+
Description:
25+
This function performs the following steps:
26+
1. Identifies local maxima in the input image.
27+
2. Determines the global maximum among these local maxima.
28+
3. Calculates the centroid of the provided binary mask.
29+
4. Optionally displays the image, mask, global maximum, centroid, and an arrow between them.
30+
31+
The function is useful for analyzing features in images, such as identifying the brightest spot and the center of a region of interest.
32+
"""
33+
34+
# Find local maxima in the image
35+
local_maxima_coords = feature.peak_local_max(image, min_distance=10)
36+
# Find the global maximum among the local maxima
37+
if len(local_maxima_coords) > 0:
38+
maxima_values = [image[coord[0], coord[1]] for coord in local_maxima_coords]
39+
global_max_index = np.argmax(maxima_values)
40+
global_max_coord = local_maxima_coords[global_max_index]
41+
else:
42+
global_max_coord = None
43+
44+
# Find the centroid of the mask using region properties
45+
properties = measure.regionprops(mask)
46+
centroid = properties[0].centroid
47+
48+
if display:
49+
fig, ax = plt.subplots(1,3)
50+
ax[0].imshow(image, cmap='gray')
51+
ax[0].set_title('Image')
52+
53+
ax[1].imshow(mask, cmap='gray')
54+
ax[1].set_title('Mask')
55+
56+
ax[2].imshow(image, cmap='gray')
57+
ax[2].plot(centroid[1], centroid[0], 'bo')
58+
ax[2].plot(global_max_coord[1], global_max_coord[0], 'ro')
59+
# Define the start and end points of the arrow
60+
start_point = (centroid[1], centroid[0])
61+
end_point = (global_max_coord[1], global_max_coord[0])
62+
plt.annotate(
63+
'', xy=end_point, xytext=start_point,
64+
arrowprops=dict(arrowstyle='->', color='red', lw=2)
65+
)
66+
ax[2].set_title('Annotated Image')
67+
68+
return global_max_coord, centroid
69+
70+
if __name__ == "__main__":
71+
pass
6.41 KB
Binary file not shown.
7.24 KB
Binary file not shown.
16.5 KB
Binary file not shown.
15.1 KB
Binary file not shown.
20.6 KB
Binary file not shown.
10.6 KB
Binary file not shown.
11.4 KB
Binary file not shown.
9.89 KB
Binary file not shown.
9.89 KB
Binary file not shown.

0 commit comments

Comments
 (0)