# Open the Drosophila larval brain sample RGB image stack #imp = IJ.openImage("https://samples.imagej.net/samples/first-instar-brain.zip") imp = IJ.getImage() stack1 = imp.getStack() # of ColorProcessor # Scale up by 1.5x scale = 1.5 model = AffineModel2D() model.set(scale, 0, 0, scale, 0, 0) # An arbitrary affine, obtained from Plugins - Transform - Interactive Affine (push enter key to print it) #model.set(1.16, 0.1484375, -0.375, 1.21875, 38.5, -39.5) # New stack, larger stack2 = ImageStack(int(imp.getWidth() * scale), int(imp.getHeight() * scale)) for index in xrange(1, stack1.getSize() + 1): stack2.addSlice(ColorProcessor(stack2.getWidth(), stack2.getHeight())) imp2 = ImagePlus("larger (push)", stack2) # Map data from stack to stack2 using the model transform position = zeros(2, 'd') """ # First approach: push (WRONG!) width1, height1 = stack1.getWidth(), stack1.getHeight() for index in xrange(1, 3): #stack1.size() + 1): cp1 = stack1.getProcessor(index) cp2 = stack2.getProcessor(index) for y in xrange(height1): for x in xrange(width1): position[1] = y
for i in range(len(tubes)): stack.addSlice("Tubeness" + str(tubes[i]), tubenesses[i].getProcessor()) for i in range(len(variances)): stack.addSlice("Variance" + str(variances[i]), imgvars[i].getProcessor()) for i in range(len(channels)): stack.addSlice("channel" + str(i + 1), channels[i].getProcessor()) del sigmas del tubes del variances del channels # create empty feature stack features = FeatureStack(stack.getWidth(), stack.getHeight(), False) # set my features to the feature stack features.setStack(stack) # put my feature stack into the array featuresArray.set(features, 0) featuresArray.setEnabledFeatures(features.getEnabledFeatures()) mp = MultilayerPerceptron() hidden_layers = "%i,%i,%i" % (20, 14, 8) mp.setHiddenLayers(hidden_layers) mp.setLearningRate(0.7) mp.setDecay(True) mp.setTrainingTime(200) mp.setMomentum(0.3)
def VesselFinder(channel_array, classifier_path): channels = channel_array image = channels[3] channels = channels[0:3] proc = image.getProcessor() directional_op = ImagePlus("directional_op", proc) tubes = range(5, 130, 12) img_source = ImagePlus("image", proc) src = clij2.push(img_source) dst = clij2.create(src) sigma = 2 clij2.gaussianBlur2D(src, dst, sigma, sigma) img_blur2 = clij2.pull(dst) src.close() dst.close() print("Tubeness mt start") tubenesses = [None] * len(tubes) rang = range(len(tubes)) threads = [] for i in rang: threads.append( threading.Thread(target=run_tube, args=(img_blur2, tubes[i], i, tubenesses))) threads[i].start() [x.join() for x in threads] print("Tubeness all done") print(tubenesses) src = clij2.push(img_source) dst = clij2.create(src) sigmas = [5, 20] imgsigmas = [] for sigma in sigmas: clij2.gaussianBlur2D(src, dst, sigma, sigma) img = clij2.pull(dst) imgsigmas.append(img) print("Gaussian Blur done") src.close() dst.close() variances = [5, 20] imgvars = [] for variance in variances: img = ImagePlus("image", proc) IJ.run(img, "Variance...", "radius=" + str(variance)) imgvars.append(img) print("Gaussian Blur done") featuresArray = FeatureStackArray(image.getStackSize()) stack = ImageStack(image.getWidth(), image.getHeight()) # add new feature here (2/2) and do not forget to add it with a # unique slice label! stack.addSlice("directional_op", directional_op.getProcessor()) for i in range(len(sigmas)): stack.addSlice("sigma" + str(sigmas[i]), imgsigmas[i].getProcessor()) for i in range(len(tubes)): stack.addSlice("Tubeness" + str(tubes[i]), tubenesses[i].getProcessor()) for i in range(len(variances)): stack.addSlice("Variance" + str(variances[i]), imgvars[i].getProcessor()) for i in range(len(channels)): stack.addSlice("channel" + str(i + 1), channels[i].getProcessor()) del sigmas del tubes del variances del channels # create empty feature stack features = FeatureStack(stack.getWidth(), stack.getHeight(), False) # set my features to the feature stack features.setStack(stack) # put my feature stack into the array featuresArray.set(features, 0) featuresArray.setEnabledFeatures(features.getEnabledFeatures()) del stack wekaSegmentation = WekaSegmentation(image) wekaSegmentation.setFeatureStackArray(featuresArray) wekaSegmentation.loadClassifier(classifier_path + "\\vessel-classifier_big.model") output = wekaSegmentation.applyClassifier(image, featuresArray, 0, True) System.gc() return output