/
FocusProjection.py
227 lines (161 loc) · 6.13 KB
/
FocusProjection.py
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import ij.IJ as IJ
import ij.ImagePlus as ImagePlus
import ij.ImageStack as ImageStack
import ij.measure.ResultsTable as ResultsTable
import ij.measure.Measurements as Measurements
import ij.process.FloatProcessor as FloatProcessor
import ij.process.ImageProcessor as ImageProcessor
import ij.plugin.ImageCalculator as ImageCalculator
import ij.plugin.ChannelSplitter as ChannelSplitter
import ij.plugin.HyperStackConverter as HyperStackConverter
import ij.plugin.ZProjector as ZProjector
import ij.plugin.filter.ParticleAnalyzer as ParticleAnalyzer
import os
import ast
import math
def readdirfiles(directory):
"""Import tiff files from a directory.
This function reads all .tiff files from a directory and it's subdirectories and returns them as a list of
hyperstacks.
Args:
directory: The path to a directory containing the tiff files.
Returns:
A list of filepaths.
"""
# Get the list of all files in directory tree at given path
listOfFiles = list()
for (dirpath, dirnames, filenames) in os.walk(directory):
listOfFiles += [os.path.join(dirpath, file) for file in filenames]
return listOfFiles
def maxfilter(floatIm, kernalSize=11):
def _kernalmax(floatIm, X, Y, kernalX, kernalY):
# Define image dimensions and kernal shape.
pixels_ = floatIm.getPixels()
width_ = int(floatIm.getWidth())
height_ = int(floatIm.getHeight())
halfX = int(kernalX / 2)
halfY = int(kernalY / 2)
startX = int(X-halfX)
startY = int(Y-halfY)
if (startX < 0):
startX = 0
if (startY < 0):
startY = 0
endX = X+halfX
endY = Y+halfY
if (endX > width_):
endX = width_
if (endY > height_):
endY = height_
# Loop through y coordinates, shift kernal for every pixel.
maxPx = 0
for y in range(startY, endY):
offset_ = width_ * y
for x in range(startX, endX):
j = offset_ + x
if pixels_[j] > maxPx:
maxPx = pixels_[j]
return maxPx
# Define input image dimensions.
width = floatIm.getWidth()
height = floatIm.getHeight()
ndim = width * height
# pixels = floatIm.getPixels()
# pixIn = [pixels[i] for i in range(len(pixels))]
# Initiate output stack with same XY dimensions as input stack.
pixOut = [0] * ndim
# Calculate Sobel transform of input image.
procIm = floatIm.convertToByteProcessor(True)
# procIm = procIm.medianFilter()
# procIm = procIm.smooth()
# procIm = procIm.findEdges()
# Loop through pixels in y dimension.
for row in range(height-1):
offset = width * row
# Within every y dimension, loop through pixels in x dimension.
for column in range(width-1):
# Retrieve maximum within kernel around pixel in sobel filtered image.
# Set pixel value in output to max of kernel in sobel image.
i = offset + column
pixOut[i] = _kernalmax(procIm, column, row, kernalSize, kernalSize)
# Return
floatOut = FloatProcessor(width, height, pixOut)
return floatOut
def imptofloat(imp):
width, height, nChannels, nSlices, nFrames = imp.getDimensions()
pix = imp.getProcessor().getPixels()
pixlist = [pix[i] for i in range(len(pix))]
# IJ.log("{}\n{}\n{}".format(pix, pix[1], pixlist))
# if min(pixlist) < 0:
# pixlist = [pixlist[i] + min(pixlist) for i in range(len(pixlist))]
# pixlist = [(pixlist[i]/max(pixlist)) for i in range(len(pixlist))]
fp = FloatProcessor(width, height, pixlist)
return fp
def depthmap(stack):
# Takes a single channel z stack.
width = stack.getWidth()
height = stack.getHeight()
# Loop through slices in stack.
size = stack.getSize()
outstack = ImageStack()
IJ.log("size: {}".format(size))
for z in range(1, size):
# Calculate maxfilter.
imslice = stack.getPixels(z)
imslice = [i for i in imslice]
imslice = FloatProcessor(width, height, imslice)
imslice = maxfilter(imslice)
outstack.addSlice(imslice)
IJ.showProgress(1.0*z/size)
# Return output stack.
return outstack
def projectfocus(instack, depthstack):
# Initialize variables.
width = instack.getWidth()
height = instack.getHeight()
nSlices = instack.getSize()
dest_pixels = [0] * width * height
# Loop through y coordinates.
for y in range(height):
offset = y*width
# Loop through x coordinates
for x in range(width):
i = offset + x
maxpx = 0.0
maxslice = 1
# Loop through z stack.
for z in range(1, nSlices):
# Find maximum pixel value
current_pixels = depthstack.getPixels(z)
current_pix = current_pixels[i]
if current_pix > maxpx:
maxslice = z
maxpx = current_pix
origin_pixels = instack.getPixels(maxslice)
# origin_pixels = [((i+min(origin_pixels))/max(origin_pixels))*255 for i in origin_pixels]# Need to get rid of negative pixels!
dest_pixels[i] = origin_pixels[i]
output = FloatProcessor(width, height, dest_pixels)
return output
def main():
# Import files.
imp = IJ.openImage("http://imagej.nih.gov/ij/images/confocal-series.zip")
# Retrieve float image.
fp = imptofloat(imp)
IJ.log("{}\n{}".format(fp, fp.medianFilter()))
# Perform focus projection.
stack = maxfilter(fp, kernalSize=11)
# Generate some output.
IJ.log("{}\n{}".format(fp, stack))
out = ImagePlus("filter", stack)
out.show()
channels = ChannelSplitter().split(imp)
channel1 = channels[0].getImageStack()
depth = depthmap(channel1)
test = ImagePlus("test", depth)
test.show()
final = projectfocus(channel1, depth)
final = ImagePlus("final", final)
final.show()
# Save file.
# imp = ImagePlus("my new image", FloatProcessor(512, 512))
main()