def ThresholdMaxEntropy(imp0): """Thresholds image and returns thresholded image, merge code still quite clumsy but functional""" imp0 = IJ.getImage() impthres = imp0.duplicate() imp01 = ImagePlus("Channel1", ChannelSplitter.getChannel(imp0, 1)) imp02 = ImagePlus("Channel2", ChannelSplitter.getChannel(imp0, 2)) imp001 = imp01.duplicate() imp002 = imp02.duplicate() IJ.setAutoThreshold(imp001, "MaxEntropy dark") IJ.run(imp001, "Convert to Mask", "") IJ.run(imp001, "Divide...", "value=255") IJ.setAutoThreshold(imp002, "MaxEntropy dark") IJ.run(imp002, "Convert to Mask", "") IJ.run(imp002, "Divide...", "value=255") ic = ImageCalculator() imp0001 = ic.run("Multiply create", imp01, imp001) ic2 = ImageCalculator() imp0002 = ic2.run("Multiply create", imp02, imp002) imp0001.copy() impthres.setC(1) impthres.paste() imp0002.copy() impthres.setC(2) impthres.paste() imp01.close() imp02.close() imp001.close() imp002.close() imp0001.close() imp0002.close() return impthres
def getCroppedChannels(self, imp, cell): splitter = ChannelSplitter() imp.setRoi(None) if cell.mode3D: cropRoi = cell.getCropRoi() else: cropRoi = cell.roi if cropRoi is None: return None crop = cropRoi.getBounds() channels = [] for c in range(1, imp.getNChannels() + 1): slices = ImageStack(crop.width, crop.height) channel = splitter.getChannel(imp, c) for z in range(1, channel.getSize() + 1): zslice = channel.getProcessor(z) zslice.setRoi(cropRoi) nslice = zslice.crop() if cell.mode3D: oroi = cell.slices[z - 1].roi else: oroi = cell.roi if oroi is not None: roi = oroi.clone() bounds = roi.getBounds() roi.setLocation(bounds.x - crop.x, bounds.y - crop.y) nslice.setColor(Color.black) nslice.fillOutside(roi) slices.addSlice(nslice) channels.append(ImagePlus("Channel %i" % c, slices)) return channels
def split_channels_blue(imp): #Create blue channel original_blue = ChannelSplitter.getChannel(imp, 3) original_blue_IP = ImagePlus(filename, original_blue) fs = FileSaver(original_blue_IP) folder = "/Users/gceleste/Desktop/test/channels" filepath = folder + "/" + "{}_bluechannel.tif".format(filename) fs.saveAsTiff(filepath) #Open blue channel image. blue = IJ.open(filepath) #blue_IP = ImagePlus(filename, blue) IJ.run( "3D Objects Counter", "threshold=100 slice=1 min.=50 max.=1447680 exclude_objects_on_edges objects summary" ) #Save blue object map blue_map = IJ.getImage() fs = FileSaver(blue_map) folder = "/Users/gceleste/Desktop/test/object maps" filepath = folder + "/" + "{}_objectmap(blue).jpg".format(filename) fs.saveAsJpeg(filepath) #Close blue channel image. blue_map.close() blue = IJ.getImage() blue.close()
def split_channels_red(imp): #Create red channel original_red = ChannelSplitter.getChannel(imp, 1) original_red_IP = ImagePlus(filename, original_red) fs = FileSaver(original_red_IP) folder = "/Users/gceleste/Desktop/test/channels" filepath = folder + "/" + "{}_redchannel.tif".format(filename) fs.saveAsTiff(filepath) #Open red channel image. red = IJ.open(filepath) #red_IP = ImagePlus(filename, red) IJ.run( "3D Objects Counter", "threshold=130 slice=1 min.=50 max.=1447680 exclude_objects_on_edges objects summary" ) #Save red object map. red_map = IJ.getImage() fs = FileSaver(red_map) folder = "/Users/gceleste/Desktop/test/object maps" filepath = folder + "/" + "{}_objectmap(red).jpg".format(filename) fs.saveAsJpeg(filepath) #Close red channel images. red_map.close() red = IJ.getImage() red.close()
def previewImage(self, imp): roi = imp.getRoi() splitter = ChannelSplitter() channels = [] for c in range(1, imp.getNChannels() + 1): channel = ImagePlus("Channel %i" % c, splitter.getChannel(imp, c)) projector = ZProjector(channel) projector.setMethod(ZProjector.MAX_METHOD) projector.doProjection() channels.append(projector.getProjection()) image = RGBStackMerge.mergeChannels(channels, False) image.title = imp.title + " MAX Intensity" image.luts = imp.luts imp.setRoi(roi) return image
def getPreview(image): enhancer = ContrastEnhancer() projector = ZProjector() splitter = ChannelSplitter() imp1 = ImagePlus("CH1", ) width, height, channels, slices, frames = image.getDimensions() chimps = [] for ch in range(1, channels + 1): projector = ZProjector(ImagePlus("C%i" % ch, splitter.getChannel(image, ch))) projector.setMethod(ZProjector.MAX_METHOD) projector.doProjection() proj = projector.getProjection() enhancer.equalize(proj) chimps.append(proj) return RGBStackMerge.mergeChannels(chimps, False)
f for f in os.listdir(os.path.join(inputPath)) if f.lower()[0].isalnum() ] for sample in imagelist: rmi.reset() IJ.run('Close All') sample_label = sample.split('.')[0] outputFile = os.path.join(outputPath, sample_label + '.zip') if not os.path.exists(outputFile): #Check that file does not yet exists print 'Segmenting ' + sample_label #Load images imp = IJ.openImage(os.path.join(inputPath, sample)) #Split channels nucleistack = cs.getChannel(imp, 1) cellstack = cs.getChannel(imp, 2) #Select relevant slices by average intensity of cell marker avgI = [] for sliceIndex in range(1, cellstack.size() + 1): #loop over slices ip = cellstack.getProcessor(sliceIndex) avgI.append(ip.getStats().mean) #get average intensity avgth1 = percentile(sorted(avgI), parameters['avgI_low']) avgth2 = percentile(sorted(avgI), parameters['avgI_high']) sel = [ avgI.index(i) + 1 for i in avgI if (i >= avgth1 and i <= avgth2) ] #pick slices with intermediate average intensity print sel #Get ROIs rois = []
results = ResultsTable() for imageFile in os.listdir(inputDir): print "Opening " + imageFile try: images = BF.openImagePlus(inputDir + imageFile) image = images[0] except UnknownFormatException: continue preview = getPreview(image) preview.show() rm = RoiManager() dialog = WaitForUserDialog("Action required", "Please select regions of interest in this image. Click OK when done.") dialog.show() rm.close() splitter = ChannelSplitter() imp1 = ImagePlus("CH1", splitter.getChannel(image, imageA)) imp2 = ImagePlus("CH2", splitter.getChannel(image, imageB)) title = image.getTitle() title = title[:title.rfind('.')] image.close() preview.close() ch1 = ImagePlusAdapter.wrap(imp1) ch2 = ImagePlusAdapter.wrap(imp2) for roi in rm.getRoisAsArray(): container = createContainer(roi, ch1, ch2) img1 = container.getSourceImage1() img2 = container.getSourceImage2() mask = container.getMask() thr1, thrimp1 = calculateThreshold(imp1, roi, methods[0])
if pixel_size is None: if objective == 20 and microscope == "SD": pixel_size = 0.5089 else: pixel_size = 1.0 files = glob.glob(input_dir + "*.tif") files.sort() frame_i = 1 for file in files: print("Processing frame \033[1m" + str(frame_i) + "\033[0m") frame = IJ.openImage(file) # ImagePlus # Get just the signal channel frame_stack = channel_splitter.getChannel(frame, channel) # ImageStack frame = ImagePlus("Channel " + str(channel), frame_stack) # Map signal to entire 16-bit range contrast_enhancer.stretchHistogram(frame, 0.01) processor = frame.getProcessor() if(processor.getBitDepth() == 32): processor.setMinAndMax(0, 1.0) else: processor.setMinAndMax(0, processor.maxValue()) frame = ImagePlus("Frame " + str(frame_i), processor) # Convert to 8-bit, grayscale converter = ImageConverter(frame) converter.convertToGray8()