-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaugment.py
More file actions
147 lines (120 loc) · 4.81 KB
/
augment.py
File metadata and controls
147 lines (120 loc) · 4.81 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import cv2
import random
import numpy as np
import os
def flip(image_path, flip_axis:bool=True):
'''
Flips an image.
Arguments:
image_path (str): path to image
flip_around_x_axis (int): 0 to flip over x axis (horizontal), 1 to flip over y axis (vertical), negative value to flip over both.
'''
image = cv2.imread(image_path)
flipped_image = cv2.flip(image, flip_axis)
return flipped_image
def flip_random(image_path):
'''
Flips an image over x axis, y axis or both of them.
Arguments:
image_path (str): path to image
'''
image = cv2.imread(image_path)
flipped_image = cv2.flip(image, random.randint(-1, 1))
cv2.imshow('flipped', flipped_image)
cv2.waitKey(0)
def blur(image_path, blur_val:int=5):
'''
Blurs an image with given blur value.
Arguments:
image_path (str): path to image
blur_val (int): blur noise value
'''
image = cv2.imread(image_path)
aug_img = cv2.blur(image,(blur_val, blur_val))
return aug_img
def shift(image_path, tx:int=1, ty:int=1):
'''
Shifts an image in x and y direction. Remves black area after shifting.
Arguments:
image_path (str): path to image
tx (int): x axis shift
ty (int): y axis shift
'''
image = cv2.imread(image_path)
rows, cols = image.shape[:2]
M = np.float32([[1, 0, tx], [0, 1, ty]])
aug_img = cv2.warpAffine(image, M, (cols, rows))
# crop black area
x, y = max(tx, 0), max(ty, 0)
w, h = cols - abs(tx), rows - abs(ty)
aug_img = aug_img[y:y+h, x:x+w]
return cv2.resize(aug_img, (cols, rows))
def rotate(image_path, angle:int):
'''
Rotates the image around its axis by a given angle.
Arguments:
image_path (str): path to image
'''
if angle < -180 or angle > 180:
print('Invalid angle...modifying')
angle = angle % 180 if angle > 0 else -(abs(angle) % 180)
print(f'using angle {angle}')
image = cv2.imread(image_path)
rows, cols = image.shape[:2]
cx, cy = rows, cols # center of rotation
M = cv2.getRotationMatrix2D((cy//2, cx//2), angle, 1)
return cv2.warpAffine(image, M, (cols, rows))
def add_noise(image_path):
'''
Adds noise to an image by converting to HSV and then generating noise.
Arguments:
image_path (str): path to image
'''
rng = np.random.default_rng(30)
image = cv2.imread(image_path)
rows, cols = image.shape[:2]
aug_img = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
h, s, v = cv2.split(aug_img)
h += rng.normal(0, 30, size=(rows, cols)).astype(np.uint8)
s += rng.normal(0, 10, size=(rows, cols)).astype(np.uint8)
v += rng.normal(0, 5, size=(rows, cols)).astype(np.uint8)
aug_img = cv2.merge([h, s, v])
return cv2.cvtColor(aug_img, cv2.COLOR_HSV2RGB)
def morphological_transform(image_path, kernel_size:int=5):
image = cv2.imread(image_path)
kernel = np.ones((kernel_size, kernel_size), np.uint8)
if random.choice([True, False]):
transformed_img = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) # Opening
#print("Applied morphological opening")
else:
transformed_img = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) # Closing
#print("Applied morphological closing")
return transformed_img
if __name__=='__main__':
input_dir = '/home/crta-hp-408/PRONOBIS/SAM_segmentator/AnnotatedDataset/masks/'
output_dir = '/home/crta-hp-408/PRONOBIS/SAM_segmentator/AnnotatedDataset/microUS_st_images1'
original_input_dir='/home/crta-hp-408/PRONOBIS/SAM_segmentator/AnnotatedDataset/images_without_annotations'
os.makedirs(output_dir, exist_ok=True)
image_files = [f for f in os.listdir(input_dir) if f.endswith(('png', 'jpg', 'jpeg'))]
#Morphological opening or closing
# for image_name in image_files:
# image_path = os.path.join(input_dir, image_name)
# print(f'Name: {image_path}')
# transformed_image = morphological_transform(image_path, 7)
# image_name = image_name.replace("gt", "st") # Replace specific text
# output_path = os.path.join(output_dir, image_name)
# cv2.imwrite(output_path, transformed_image)
# print(f'Saved: {output_path}')
for image_name in image_files:
image_path = os.path.join(input_dir, image_name)
#print(image_path)
original_image = image_name.replace("gt", "img")
original_image_path=os.path.join(original_input_dir,original_image)
#print(original_image_path)
try:
original_image=cv2.imread(original_image_path)
mask_image=cv2.imread(image_path)
#print("OK")
except:
print("No Images!!!!!!!!!!!!!!!!")
#print(original_image_path)