[db7631]: / Classification / crop_roi_axial.py

Download this file

139 lines (108 with data), 4.2 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
import os
import xml.etree.ElementTree as ET
import cv2
cwd = './Axial_1-491_10751x2jpgXmls_ResnetRecrop_Jun152020'
center_dir = 'center-crop'
left_dir = 'left-crop'
right_dir = 'right-crop'
out_dirs = [center_dir, left_dir, right_dir]
grading = ['1', '2', '3', '4']
center_label_file = 'Axial_1-491_10751x2_Jun142020-axial-center-label.txt'
right_label_file = 'Axial_1-491_10751x2_Jun142020-axial-right-label.txt'
left_label_file = 'Axial_1-491_10751x2_Jun142020-axial-left-label.txt'
# 150% scale, use scale_factor = 0.5
scale_factor = 0.5
for e in out_dirs:
if not os.path.exists(e):
os.makedirs(e)
for ee in grading:
if not os.path.exists(os.path.join(e, ee)):
os.makedirs(os.path.join(e, ee))
center_label = {}
right_label = {}
left_label = {}
with open(center_label_file, 'r') as f:
lines = f.readlines()
for e in lines:
tmp = e.strip().split(' ')
center_label[tmp[0]] = tmp[1]
with open(right_label_file, 'r') as f:
lines = f.readlines()
for e in lines:
tmp = e.strip().split(' ')
right_label[tmp[0]] = tmp[1]
with open(left_label_file, 'r') as f:
lines = f.readlines()
for e in lines:
tmp = e.strip().split(' ')
left_label[tmp[0]] = tmp[1]
# print(center_label)
# print(right_label)
# print(left_label)
# exit(0)
def scale_crop(xmin, xmax, ymin, ymax, factor, img_shape):
"""
the imgs top left corner is (0,0),
x min-max is from point A,
y min-max is from point B
"""
cropped_w = xmax - xmin
cropped_h = ymax - ymin
xmin -= ((cropped_w * factor) // 2)
ymin -= ((cropped_h * factor) // 2)
xmax += ((cropped_w * factor) // 2)
ymax += ((cropped_h * factor) // 2)
# cv2 img shape information
height = img_shape[0]
width = img_shape[1]
return (
int(max(xmin, 0)),
int(min(xmax, width)),
int(max(ymin, 0)),
int(min(ymax, height))
)
for file in os.listdir(cwd):
if '.xml' in file:
print(file)
img = cv2.imread(os.path.join(cwd, file.replace('xml', 'JPG')), 0)
print(img.shape)
# print(np.array_equal(img[:,:,0], img[:,:,1]))
# print(np.array_equal(img[:,:,0], img[:,:,2]))
# print(np.array_equal(img[:,:,1], img[:,:,2]))
# for k in range(0, 3):
# for i in range(0, 10):
# tmp = ''
# for j in range(0, 10):
# tmp += str(img[i,j,k]) + ' '
# print(tmp + '\n')
# print('\n\n')
data = ET.parse(os.path.join(cwd, file))
root = data.getroot()
for o in root.findall('object'):
name = o.find('name').text
xmin = int(o.find('bndbox').find('xmin').text)
xmax = int(o.find('bndbox').find('xmax').text)
ymin = int(o.find('bndbox').find('ymin').text)
ymax = int(o.find('bndbox').find('ymax').text)
print(">>>>before scaling, xmin, xmax, ymin, ymax: ", xmin, xmax, ymin, ymax)
(xmin, xmax, ymin, ymax) = scale_crop(xmin, xmax, ymin, ymax, scale_factor, img.shape)
print("<<<<after scaling, xmin, xmax, ymin, ymax: ", xmin, xmax, ymin, ymax)
cropped_img = img[ymin:ymax, xmin:xmax]
# cv2.imshow('image', cropped_img)
# cv2.waitKey(0)
# exit(0)
# print(file)
# exit(0)
if name == 'center':
tmp = center_label[file]
if not cv2.imwrite(os.path.join(center_dir, tmp, file.replace('xml', 'png')), cropped_img):
print(os.path.join(center_dir, tmp, file.replace('xml', 'jpg')))
raise Exception("Could not write image")
elif name == 'right':
tmp = right_label[file]
if not cv2.imwrite(os.path.join(right_dir, tmp, file.replace('xml', 'png')), cropped_img):
raise Exception("Could not write image")
else:
tmp = left_label[file]
if not cv2.imwrite(os.path.join(left_dir, tmp, file.replace('xml', 'png')), cropped_img):
raise Exception("Could not write image")