-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_distal_TF_junction_slices_registrated.py
More file actions
114 lines (87 loc) · 5.06 KB
/
select_distal_TF_junction_slices_registrated.py
File metadata and controls
114 lines (87 loc) · 5.06 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
import numpy as np
from skimage import io
import cv2
import os
import matplotlib.pyplot as plt
def midshaft(img2d):
image = img2d
#smoothing images
image=np.array(image)
ret, thresh = cv2.threshold(image, 75, 255, cv2.THRESH_BINARY)
smoothedimage = cv2.medianBlur(thresh,21) #45
# smootherfilter = sitk.SmoothingRecursiveGaussianImageFilter()
# smootherfilter.SetSigma(2.5)
# smootherfilter.SetNormalizeAcrossScale(1)
# smoothedimage = smootherfilter.Execute(image)
# Define the region (mask) at the bottom of the image where particles are located
height, width = smoothedimage.shape
mask_height = int(0.2*height) # Define how tall the mask is from the bottom
mask = np.zeros_like(thresh)
mask[height-mask_height:height, :] = 1
# Remove the small particles by applying the mask
smoothedimage[mask == 1] = 0
# plt.imshow(smoothedimage,cmap="gray")
# plt.show()
#identify the mid-shaft position
_,binary = cv2.threshold(smoothedimage,50,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_NONE) #cv2.RETR_EXTERNAL
count= len(contours)
if count == 1 :
return False
elif count > 1 :
# cv2.drawContours(smoothedimage, contours, -1, 255, 2, lineType=cv2.LINE_AA)
# cv2.imshow('example', smoothedimage)
# cv2.waitKey(0)
# cv2.imwrite('contours_none_image1.jpg', smoothedimage)
# cv2.destroyAllWindows()
return True
config = {
"foldername" : "selected above distal t-f junction 50-100" , #50-100, 450-500 955
"slice_above_mid_shaft_folder" : "first slice selected",
"first_slice_selected_above_t-f_junction" : 50 #x: first selected slice index above mid-shaft :50, 450
}
input= r"E:\PD mice 120823\regis\Tibia w0w11composite" #folder of input composite w0w_composite
foldername = config["foldername"]
slice_above_mid_shaft_folder = config["slice_above_mid_shaft_folder"] # we select 50 slices each time,
first_slice_selected_above_mid_shaft = config["first_slice_selected_above_t-f_junction"] #the # of slices starting select above tf- junction
if not os.path.exists(os.path.join(input,foldername)):
os.mkdir(os.path.join(input,foldername))
if not os.path.exists(os.path.join(input,foldername,"slice of t-f junction")):
os.mkdir(os.path.join(input,foldername,"slice of t-f junction"))
if not os.path.exists(os.path.join(input,foldername,slice_above_mid_shaft_folder)):
os.mkdir(os.path.join(input,foldername,slice_above_mid_shaft_folder))
for folder in os.listdir(input):
if not os.path.exists(os.path.join(input,foldername,folder)):
os.mkdir(os.path.join(input,foldername,folder))
folderpath=os.path.join(input,folder)
try:
img=[]
for filename in os.listdir(folderpath):
if filename.endswith('.tif'):
rawimage = cv2.imread(os.path.join(folderpath,filename),0)
#rawimage = cv2.cvtColor(rawimage, cv2.COLOR_BGR2GRAY)
img.append(rawimage)
for i in range(len(img)): # shape[0]: read image in grayscale
#shape = [z,y,x] 3d
if i > 350: #385
img2d = img[i]
if midshaft(img2d):
# select slice at mid-shaft and last image selected
io.imsave(os.path.join(input,foldername,"slice of t-f junction",os.listdir(folderpath)[i]),img[i]) # output images of t-f junction
io.imsave(os.path.join(input,foldername,slice_above_mid_shaft_folder,os.listdir(folderpath)[i+first_slice_selected_above_mid_shaft]),img[i + first_slice_selected_above_mid_shaft])
#io.imsave(os.path.join(input,foldername,slice_above_mid_shaft_folder,os.listdir(folderpath)[i+first_slice_selected_above_mid_shaft+50]),img[i + first_slice_selected_above_mid_shaft +50]) # output last one of selected 50 slices of mid-shaft
# select 50 slices from xxx (ex.450) slices above mid-shaft
j=i+first_slice_selected_above_mid_shaft # j=i+x x: first selected slice index ############################### start select
for k in range(50): #x : # of slices selected
per_selected_img= img[j+k]
io.imsave(os.path.join(input,foldername,folder,os.listdir(folderpath)[j+k]),per_selected_img) # output images
break
except:
print("Error happens:",folder)
#folder = r"C:\Users\SplLabMEEG\Desktop\03.07.2022 Aged non tumor Yoda1 Veh loading\selected mice\5. mkcomposite\Tibia w0w4composite\101 week 4 left tibia w0w4composite" #shape = [z,y,x] 3d
#img=[]
# for filename in os.listdir(folder):
# rawimage = cv2.imread(os.path.join(folder,filename),0)
# #rawimage = cv2.cvtColor(rawimage, cv2.COLOR_BGR2GRAY)
# img.append(rawimage)
#save images