-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFindBoundingbox.py
More file actions
115 lines (105 loc) · 4.14 KB
/
Copy pathFindBoundingbox.py
File metadata and controls
115 lines (105 loc) · 4.14 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
'''
Author: Jing Zhang
Date: 2021-01-14
Function:
1.Find boundingbox of each patient according to ground truth.
2.Find the largest boundingbox of each slice.
3.Crop the image according to the current largest boundingbox.
4.Do the same operation to 200 patients.
5.Then make borders of each cropped patients.
'''
## using simpleITK to load and save data.
import SimpleITK as sitk
import glob, os
import numpy as np
import cv2
def Info(images_path):
imagelist = sorted(glob.glob(os.path.join(images_path, '*.nii.gz'))) # sorted按名称排序,glob.glob 匹配,os.path.join字符串拼接
for i in (range(len(imagelist))):
#print(imagelist[i][11:])
itk_img = sitk.ReadImage(imagelist[i])
img = sitk.GetArrayFromImage(itk_img)
#print('original:', img.shape)
print(img.shape)
path ='ACDC_3D_FindBoundbox/img_cropped/'
Info(path)
def boundingbox(src):
#src = cv2.imread(src)
#contours = cv2.findContours(src, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = cv2.Canny(src,0,1)
x, y, w, h = cv2.boundingRect(contours)
#print(x,y,w,h)
cv2.rectangle(src, (x, y), (x + w, y + h), (100,10,200), 1, 8, 0)
cv2.imshow('bb',src)
#cv2.waitKey(0)
return x,y,w,h
def Findboundingbox(images_path,i):
imagelist = sorted(glob.glob(os.path.join(images_path, '*.nii.gz')))
print(imagelist[i])
itk_img = sitk.ReadImage(imagelist[i])
img = sitk.GetArrayFromImage(itk_img)
min_x1 = 100
min_y1 = 100
max_x2 = 100
max_y2 = 100
max_width = 20
max_height = 20
for k in range(0,img.shape[0]):# for each slice
x, y, w, h = boundingbox(img[k])
max_xw = x + w
max_yh = y + h
if (x < min_x1 and x != 0): min_x1 = x
if (y < min_y1 and y != 0): min_y1 = y
if (max_xw > max_x2): max_x2 = max_xw
if (max_yh > max_y2): max_y2 = max_yh
if (w>max_width):max_width = w
if (h>max_height):max_height = h
print('the biggest boundingbox:', min_x1, min_y1, max_x2, max_y2)
#print('the biggest boundingbox:', max_width,max_height)
return min_x1, min_y1, max_x2, max_y2
def CropnMakeBorder(images_path,i):
min_x1, min_y1, max_x2, max_y2 = Findboundingbox(src_gt,i)# the boundingbox of ground truth
imagelist = sorted(glob.glob(os.path.join(images_path, '*.nii.gz')))
print(imagelist[i])
itk_img = sitk.ReadImage(imagelist[i])
img = sitk.GetArrayFromImage(itk_img)
width = max_x2-min_x1
height = max_y2-min_y1
cropped_patch = np.zeros(((len(img), height, width)))#(slice,183,147)
print("The shape of cropped image: ",cropped_patch.shape)
for k in range(0,img.shape[0]):# for each slice
#print(img[k].shape)
cropped_patch[k] = img[k][min_y1:max_y2, min_x1:max_x2]
cv2.imshow('bb', cropped_patch[k].astype('uint8') * 255)
#cv2.waitKey(0)
img = sitk.GetImageFromArray(cropped_patch)
sitk.WriteImage(img, imagelist[i])
src_gt = 'ACDC_3D/gt/'
src_img = 'ACDC_3D/img/'
n=0
# while(n<200):
# print("*****The patient number: *****", n)
# CropnMakeBorder(src_img,n)
# n=n+1
def MakeBorder(images_path):
imagelist = sorted(glob.glob(os.path.join(images_path, '*.nii.gz'))) # sorted按名称排序,glob.glob 匹配,os.path.join字符串拼接
for i in (range(len(imagelist))) :
#print(imagelist[i])
itk_img = sitk.ReadImage(imagelist[i])
img = sitk.GetArrayFromImage(itk_img)
height = 235
width = 167
patch = np.zeros(((len(img), height, width))) # (slice,183,147)
for k in range(0,img.shape[0]):# for each slice
print(img[k].shape)
if img[k].shape[1]<width or img[k].shape[0]<height:
patch[k] = cv2.copyMakeBorder(img[k], 0,
np.abs(height-img[k].shape[0]), 0, np.abs(width-img[k].shape[1]) , cv2.BORDER_CONSTANT,
value=(0, 0, 0)) # top, bottom, left, right,
print(patch.shape)
cv2.imshow('bb', patch[k].astype('uint8') * 255)
#cv2.waitKey(0)
img = sitk.GetImageFromArray(patch)
sitk.WriteImage(img, imagelist[i])
src = 'ACDC_3D/img_cropped/'
#MakeBorder(src)