comparison points2label.py @ 3:2ae122d5d85a draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
author imgteam
date Wed, 23 Apr 2025 14:37:31 +0000
parents 714a57d6f3a1
children 64c155acb864
comparison
equal deleted inserted replaced
2:714a57d6f3a1 3:2ae122d5d85a
31 assert len(pos_x_list) == len(radius_list) 31 assert len(pos_x_list) == len(radius_list)
32 except KeyError: 32 except KeyError:
33 radius_list = [0] * len(pos_x_list) 33 radius_list = [0] * len(pos_x_list)
34 34
35 try: 35 try:
36 width_column = giatools.pandas.find_column(df, ['width', 'WIDTH'])
37 height_column = giatools.pandas.find_column(df, ['height', 'HEIGHT'])
38 width_list = df[width_column]
39 height_list = df[height_column]
40 assert len(pos_x_list) == len(width_list)
41 assert len(pos_x_list) == len(height_list)
42 except KeyError:
43 width_list = [0] * len(pos_x_list)
44 height_list = [0] * len(pos_x_list)
45
46 try:
36 label_column = giatools.pandas.find_column(df, ['label', 'LABEL']) 47 label_column = giatools.pandas.find_column(df, ['label', 'LABEL'])
37 label_list = df[label_column] 48 label_list = df[label_column]
38 assert len(pos_x_list) == len(label_list) 49 assert len(pos_x_list) == len(label_list)
39 except KeyError: 50 except KeyError:
40 label_list = list(range(1, len(pos_x_list) + 1)) 51 label_list = list(range(1, len(pos_x_list) + 1))
43 else: 54 else:
44 df = pd.read_csv(point_file, header=None, delimiter='\t') 55 df = pd.read_csv(point_file, header=None, delimiter='\t')
45 pos_x_list = df[0].round().astype(int) 56 pos_x_list = df[0].round().astype(int)
46 pos_y_list = df[1].round().astype(int) 57 pos_y_list = df[1].round().astype(int)
47 assert len(pos_x_list) == len(pos_y_list) 58 assert len(pos_x_list) == len(pos_y_list)
48 radius_list = [0] * len(pos_x_list) 59 radius_list, width_list, height_list = [[0] * len(pos_x_list)] * 3
49 label_list = list(range(1, len(pos_x_list) + 1)) 60 label_list = list(range(1, len(pos_x_list) + 1))
50 61
51 # Optionally swap the coordinates 62 # Optionally swap the coordinates
52 if swap_xy: 63 if swap_xy:
53 pos_x_list, pos_y_list = pos_y_list, pos_x_list 64 pos_x_list, pos_y_list = pos_y_list, pos_x_list
54 65
55 # Perform the rasterization 66 # Perform the rasterization
56 for y, x, radius, label in zip(pos_y_list, pos_x_list, radius_list, label_list): 67 for y, x, radius, width, height, label in zip(
68 pos_y_list, pos_x_list, radius_list, width_list, height_list, label_list,
69 ):
57 if fg_value is not None: 70 if fg_value is not None:
58 label = fg_value 71 label = fg_value
59 72
60 if y < 0 or x < 0 or y >= shape[0] or x >= shape[1]: 73 if y < 0 or x < 0 or y >= shape[0] or x >= shape[1]:
61 raise IndexError(f'The point x={x}, y={y} exceeds the bounds of the image (width: {shape[1]}, height: {shape[0]})') 74 raise IndexError(f'The point x={x}, y={y} exceeds the bounds of the image (width: {shape[1]}, height: {shape[0]})')
62 75
63 # Rasterize circle and distribute overlapping image area 76 # Rasterize circle and distribute overlapping image area
64 if radius > 0: 77 # Rasterize primitive geometry
65 mask = np.ones(shape, dtype=bool) 78 if radius > 0 or (width > 0 and height > 0):
66 mask[y, x] = False 79
67 mask = (ndi.distance_transform_edt(mask) <= radius) 80 # Rasterize circle
81 if radius > 0:
82 mask = np.ones(shape, dtype=bool)
83 mask[y, x] = False
84 mask = (ndi.distance_transform_edt(mask) <= radius)
85 else:
86 mask = np.zeros(shape, dtype=bool)
87
88 # Rasterize rectangle
89 if width > 0 and height > 0:
90 mask[
91 y:min(shape[0], y + width),
92 x:min(shape[1], x + height)
93 ] = True
68 94
69 # Compute the overlap (pretend there is none if the rasterization is binary) 95 # Compute the overlap (pretend there is none if the rasterization is binary)
70 if fg_value is None: 96 if fg_value is None:
71 overlap = np.logical_and(img > 0, mask) 97 overlap = np.logical_and(img > 0, mask)
72 else: 98 else: