本文整理汇总了Python中ij.ImagePlus.getNSlices方法的典型用法代码示例。如果您正苦于以下问题:Python ImagePlus.getNSlices方法的具体用法?Python ImagePlus.getNSlices怎么用?Python ImagePlus.getNSlices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ij.ImagePlus
的用法示例。
在下文中一共展示了ImagePlus.getNSlices方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_registered_hyperstack
# 需要导入模块: from ij import ImagePlus [as 别名]
# 或者: from ij.ImagePlus import getNSlices [as 别名]
def create_registered_hyperstack(imp, target_folder, channel):
""" Takes the imp, which contains a virtual hyper stack,
and determines the x,y,z drift for each pair of time points,
using the preferred given channel,
and output one image for each slide into the target folder."""
print "starting to calculate translations..."
shifts = compute_frame_translations(imp, channel)
# Make shifts relative to 0,0,0 of the original imp:
shifts = concatenate_shifts(shifts)
print "shifts concatenated:"
for s in shifts:
print s.x, s.y, s.z
# Compute bounds of the new volume,
# which accounts for all translations:
minx, miny, minz, maxx, maxy, maxz = compute_min_max(shifts)
# Make shifts relative to new canvas dimensions
# so that the min values become 0,0,0
for shift in shifts:
shift.x -= minx
shift.y -= miny
shift.z -= minz
print "shifts relative to new dimensions:"
for s in shifts:
print s.x, s.y, s.z
writer = CSVWriter(FileWriter(target_folder+"/shifts.csv"), ',')
data = Array.newInstance(Class.forName("java.lang.String"), 3)
for s in shifts:
data[0] = str(s.x)
data[1] = str(s.y)
data[2] = str(s.z)
writer.writeNext(data)
writer.close()
# new canvas dimensions:
width = imp.width + maxx - minx
height = maxy - miny + imp.height
slices = maxz - minz + imp.getNSlices()
print "New dimensions:", width, height, slices
# Count number of digits of each dimension, to output zero-padded numbers:
slice_digits = len(str(slices))
frame_digits = len(str(imp.getNFrames()))
channel_digits = len(str(imp.getNChannels()))
# List to accumulate all created names:
names = []
# Prepare empty slice to pad in Z when necessary
empty = imp.getProcessor().createProcessor(width, height)
# if it's RGB, fill the empty slice with blackness
if isinstance(empty, ColorProcessor):
empty.setValue(0)
empty.fill()
# Write all slices to files:
stack = imp.getStack()
for frame in range(1, imp.getNFrames()+1):
shift = shifts[frame-1]
fr = "t" + zero_pad(frame, frame_digits)
# Pad with mpty slices before reaching the first slice
for s in range(shift.z):
ss = "_z" + zero_pad(s + 1, slice_digits) # slices start at 1
for ch in range(1, imp.getNChannels()+1):
name = fr + ss + "_c" + zero_pad(ch, channel_digits) +".tif"
names.append(name)
FileSaver(ImagePlus("", empty)).saveAsTiff(target_folder + "/" + name)
# Add all proper slices
for s in range(1, imp.getNSlices()+1):
ss = "_z" + zero_pad(s + shift.z, slice_digits)
for ch in range(1, imp.getNChannels()+1):
ip = stack.getProcessor(imp.getStackIndex(ch, s, frame))
ip2 = ip.createProcessor(width, height) # potentially larger
ip2.insert(ip, shift.x, shift.y)
name = fr + ss + "_c" + zero_pad(ch, channel_digits) +".tif"
names.append(name)
FileSaver(ImagePlus("", ip2)).saveAsTiff(target_folder + "/" + name)
# Pad the end
for s in range(shift.z + imp.getNSlices(), slices):
ss = "_z" + zero_pad(s + 1, slice_digits)
for ch in range(1, imp.getNChannels()+1):
name = fr + ss + "_c" + zero_pad(ch, channel_digits) +".tif"
names.append(name)
FileSaver(ImagePlus("", empty)).saveAsTiff(target_folder + "/" + name)
# Create virtual hyper stack with the result
vs = VirtualStack(width, height, None, target_folder)
for name in names:
vs.addSlice(name)
vs_imp = ImagePlus("registered time points", vs)
vs_imp.setDimensions(imp.getNChannels(), len(names) / (imp.getNChannels() * imp.getNFrames()), imp.getNFrames())
vs_imp.setOpenAsHyperStack(True)
IJ.log("\nHyperstack dimensions: time frames:" + str(vs_imp.getNFrames()) + ", slices: " + str(vs_imp.getNSlices()) + ", channels: " + str(vs_imp.getNChannels()))
if 1 == vs_imp.getNSlices():
return vs_imp
# Else, as composite
mode = CompositeImage.COLOR;
if isinstance(imp, CompositeImage):
mode = imp.getMode()
else:
return vs_imp
return CompositeImage(vs_imp, mode)
示例2: create_registered_hyperstack
# 需要导入模块: from ij import ImagePlus [as 别名]
# 或者: from ij.ImagePlus import getNSlices [as 别名]
def create_registered_hyperstack(imp, channel, target_folder, virtual):
""" Takes the imp, determines the x,y,z drift for each pair of time points, using the preferred given channel,
and outputs as a hyperstack."""
shifts = compute_frame_translations(imp, channel)
# Make shifts relative to 0,0,0 of the original imp:
shifts = concatenate_shifts(shifts)
print "shifts concatenated:"
for s in shifts:
print s.x, s.y, s.z
# Compute bounds of the new volume,
# which accounts for all translations:
minx, miny, minz, maxx, maxy, maxz = compute_min_max(shifts)
# Make shifts relative to new canvas dimensions
# so that the min values become 0,0,0
for shift in shifts:
shift.x -= minx
shift.y -= miny
shift.z -= minz
print "shifts relative to new dimensions:"
for s in shifts:
print s.x, s.y, s.z
# new canvas dimensions:
width = imp.width + maxx - minx
height = maxy - miny + imp.height
slices = maxz - minz + imp.getNSlices()
print "New dimensions:", width, height, slices
# Prepare empty slice to pad in Z when necessary
empty = imp.getProcessor().createProcessor(width, height)
# if it's RGB, fill the empty slice with blackness
if isinstance(empty, ColorProcessor):
empty.setValue(0)
empty.fill()
# Write all slices to files:
stack = imp.getStack()
if virtual is False:
registeredstack = ImageStack(width, height, imp.getProcessor().getColorModel())
names = []
for frame in range(1, imp.getNFrames()+1):
shift = shifts[frame-1]
fr = "t" + zero_pad(frame, len(str(imp.getNFrames())))
# Pad with empty slices before reaching the first slice
for s in range(shift.z):
ss = "_z" + zero_pad(s + 1, len(str(slices))) # slices start at 1
for ch in range(1, imp.getNChannels()+1):
name = fr + ss + "_c" + zero_pad(ch, len(str(imp.getNChannels()))) +".tif"
names.append(name)
if virtual is True:
currentslice = ImagePlus("", empty)
currentslice.setCalibration(imp.getCalibration().copy())
currentslice.setProperty("Info", imp.getProperty("Info"))
FileSaver(currentslice).saveAsTiff(target_folder + "/" + name)
else:
empty = imp.getProcessor().createProcessor(width, height)
registeredstack.addSlice(str(name), empty)
# Add all proper slices
stack = imp.getStack()
for s in range(1, imp.getNSlices()+1):
ss = "_z" + zero_pad(s + shift.z, len(str(slices)))
for ch in range(1, imp.getNChannels()+1):
ip = stack.getProcessor(imp.getStackIndex(ch, s, frame))
ip2 = ip.createProcessor(width, height) # potentially larger
ip2.insert(ip, shift.x, shift.y)
name = fr + ss + "_c" + zero_pad(ch, len(str(imp.getNChannels()))) +".tif"
names.append(name)
if virtual is True:
currentslice = ImagePlus("", ip2)
currentslice.setCalibration(imp.getCalibration().copy())
currentslice.setProperty("Info", imp.getProperty("Info"));
FileSaver(currentslice).saveAsTiff(target_folder + "/" + name)
else:
registeredstack.addSlice(str(name), ip2)
# Pad the end
for s in range(shift.z + imp.getNSlices(), slices):
ss = "_z" + zero_pad(s + 1, len(str(slices)))
for ch in range(1, imp.getNChannels()+1):
name = fr + ss + "_c" + zero_pad(ch, len(str(imp.getNChannels()))) +".tif"
names.append(name)
if virtual is True:
currentslice = ImagePlus("", empty)
currentslice.setCalibration(imp.getCalibration().copy())
currentslice.setProperty("Info", imp.getProperty("Info"))
FileSaver(currentslice).saveAsTiff(target_folder + "/" + name)
else:
registeredstack.addSlice(str(name), empty)
if virtual is True:
# Create virtual hyper stack with the result
registeredstack = VirtualStack(width, height, None, target_folder)
for name in names:
registeredstack.addSlice(name)
registeredstack_imp = ImagePlus("registered time points", registeredstack)
registeredstack_imp.setDimensions(imp.getNChannels(), len(names) / (imp.getNChannels() * imp.getNFrames()), imp.getNFrames())
registeredstack_imp.setCalibration(imp.getCalibration().copy())
#.........这里部分代码省略.........
示例3: CreateCover
# 需要导入模块: from ij import ImagePlus [as 别名]
# 或者: from ij.ImagePlus import getNSlices [as 别名]
def CreateCover(ip, width, height, dbpath):
# split input image into appropriate tiles
stackt = SplitImage(ip, width, height)
impt = ImagePlus("template", stackt)
nSlicestmp = impt.getNSlices()
# open the preprocessed database
print dbpath
impd = IJ.openImage(dbpath)
stackd = impd.getImageStack()
nSlicesdb = impd.getNSlices()
#associate index with image names
imageNames = impd.getProperty('Info')
imageList = imageNames.split(';')
# set up preview output
outputip = ColorProcessor(ip.width, ip.height)
outputimp = ImagePlus("output", outputip)
outputimp.show()
cols = ip.width/width
rows = ip.height/height
print str(cols) + "," + str(rows)
x = 0
y = 0
arrays = [None, None] # a list of two elements
cruncher = Inline(arrays)
tileNames = {}
tileIndex = {}
placed = {}
used = {}
while len(placed) < nSlicestmp:
randomTileIndex = random.randint(1, nSlicestmp)
if randomTileIndex in placed:
continue
# transform to row adn column coordinate
if randomTileIndex%rows == 0:
y = rows-1
x = (randomTileIndex/rows)-1
else:
y = (randomTileIndex%rows)-1
x = int(randomTileIndex/rows)
pixelst = stackt.getPixels(randomTileIndex)
minimum = Float.MAX_VALUE
#iterate through database images
j = 1
indexOfBestMatch = 0
arrays[0] = pixelst
while j < nSlicesdb:
if j in used:
j +=1
continue
arrays[1] = stackd.getPixels(j)
diff = cruncher.call()
if diff < minimum:
minimum = diff
indexOfBestMatch = j
j += 1
ip = stackd.getProcessor(indexOfBestMatch)
outputip.copyBits(ip, x*width, y*height, 0)
used[indexOfBestMatch] = 1
tileNames[randomTileIndex] = imageList[indexOfBestMatch-1]
tileIndex[randomTileIndex] = indexOfBestMatch-1
outputimp.draw()
placed[randomTileIndex] = 1
return tileNames, tileIndex, cols, rows