/** * Execute plugin functionality: stack FFT with window function, max projection over all slices * (phase, Z angle), blank out central 1/8 circle (set to min value), display min-max. */ public ResultSet exec(ImagePlus... imps) { ImagePlus imp = imps[0]; Util_StackFFT2D stackFFT2D = new Util_StackFFT2D(); stackFFT2D.resultTypeChoice = Util_StackFFT2D.resultType[1]; ImagePlus impF = stackFFT2D.exec(imp); IJ.run(impF, "Z Project...", "projection=[Max Intensity]"); ImagePlus impProjF = ij.WindowManager.getCurrentImage(); maskCentralRegion(impProjF); if (impProjF.isComposite()) { // display grayscale, not colored composite CompositeImage ci = (CompositeImage) impProjF; ci.setMode(IJ.GRAYSCALE); impProjF.updateAndDraw(); } displayMinToMax(impProjF); impProjF.setTitle(I1l.makeTitle(imps[0], TLA)); String shortInfo = "Maximum intensity projection of log" + " (amplitude^2) 2D FFT stack, central region masked," + " rescaled (min-max) to improve contrast of the relevant" + " frequency range."; results.addImp(shortInfo, impProjF); results.addInfo( "How to interpret", "look for clean 1st & 2nd" + " order spots, similar across angles. Note that spot" + " intensity depends on image content."); return results; }
public void update() { CompositeImage ci = getImage(); if (ci == null || checkbox == null) return; int n = checkbox.length; int nChannels = ci.getNChannels(); if (nChannels != n && nChannels <= CompositeImage.MAX_CHANNELS) { instance = null; location = getLocation(); close(); new Channels(); return; } boolean[] active = ci.getActiveChannels(); for (int i = 0; i < checkbox.length; i++) checkbox[i].setState(active[i]); int index = 0; switch (ci.getMode()) { case IJ.COMPOSITE: index = 0; break; case IJ.COLOR: index = 1; break; case IJ.GRAYSCALE: index = 2; break; } choice.select(index); }
public Channels() { super("Channels"); if (instance != null) { instance.toFront(); return; } WindowManager.addWindow(this); instance = this; GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridbag); int y = 0; c.gridx = 0; c.gridy = y++; c.gridwidth = 1; c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.CENTER; int margin = 32; if (IJ.isMacOSX()) margin = 20; c.insets = new Insets(10, margin, 10, margin); choice = new Choice(); for (int i = 0; i < modes.length; i++) choice.addItem(modes[i]); choice.select(0); choice.addItemListener(this); add(choice, c); CompositeImage ci = getImage(); int nCheckBoxes = ci != null ? ci.getNChannels() : 3; if (nCheckBoxes > CompositeImage.MAX_CHANNELS) nCheckBoxes = CompositeImage.MAX_CHANNELS; checkbox = new Checkbox[nCheckBoxes]; for (int i = 0; i < nCheckBoxes; i++) { checkbox[i] = new Checkbox("Channel " + (i + 1), true); c.insets = new Insets(0, 25, i < nCheckBoxes - 1 ? 0 : 10, 5); c.gridy = y++; add(checkbox[i], c); checkbox[i].addItemListener(this); } c.insets = new Insets(0, 15, 10, 15); c.fill = GridBagConstraints.NONE; c.gridy = y++; moreButton = new Button(moreLabel); moreButton.addActionListener(this); add(moreButton, c); update(); pm = new PopupMenu(); for (int i = 0; i < menuItems.length; i++) addPopupItem(menuItems[i]); add(pm); addKeyListener(IJ.getInstance()); // ImageJ handles keyboard shortcuts setResizable(false); pack(); if (location == null) { GUI.center(this); location = getLocation(); } else setLocation(location); show(); }
/** * Copies the LUTs and display mode of 'imp' to this image. Does nothing if 'imp' is not a * CompositeImage or 'imp' and this image do not have the same number of channels. */ public synchronized void copyLuts(ImagePlus imp) { int channels = getNChannels(); if (!imp.isComposite() || imp.getNChannels() != channels) return; CompositeImage ci = (CompositeImage) imp; LUT[] luts = ci.getLuts(); if (luts != null && luts.length == channels) { lut = luts; cip = null; } int mode2 = ci.getMode(); setMode(mode2); if (mode2 == COMPOSITE) { boolean[] active2 = ci.getActiveChannels(); for (int i = 0; i < MAX_CHANNELS; i++) active[i] = active2[i]; } if (ci.hasCustomLuts()) customLuts = true; }
public static void showLut(ImagePlus imp, int channel, FileInfo fi, boolean showImage) { if (imp != null) { if (imp.getType() == ImagePlus.COLOR_RGB) IJ.error("Color tables cannot be assiged to RGB Images."); else { IndexColorModel cm = null; cm = new IndexColorModel(8, 256, fi.reds, fi.greens, fi.blues); imp.setPosition(channel + 1, imp.getSlice(), imp.getFrame()); if (imp.isComposite()) { ((CompositeImage) imp).setChannelColorModel(cm); ((CompositeImage) imp).updateChannelAndDraw(); } else { imp.getProcessor().setColorModel(cm); imp.updateAndDraw(); } } } }
public void doHyperStackProjection(boolean allTimeFrames) { int start = startSlice; int stop = stopSlice; int firstFrame = 1; int lastFrame = imp.getNFrames(); if (!allTimeFrames) firstFrame = lastFrame = imp.getFrame(); ImageStack stack = new ImageStack(imp.getWidth(), imp.getHeight()); int channels = imp.getNChannels(); int slices = imp.getNSlices(); if (slices == 1) { slices = imp.getNFrames(); firstFrame = lastFrame = 1; } int frames = lastFrame - firstFrame + 1; increment = channels; boolean rgb = imp.getBitDepth() == 24; for (int frame = firstFrame; frame <= lastFrame; frame++) { for (int channel = 1; channel <= channels; channel++) { startSlice = (frame - 1) * channels * slices + (start - 1) * channels + channel; stopSlice = (frame - 1) * channels * slices + (stop - 1) * channels + channel; if (rgb) doHSRGBProjection(imp); else doProjection(); stack.addSlice(null, projImage.getProcessor()); } } projImage = new ImagePlus(makeTitle(), stack); projImage.setDimensions(channels, 1, frames); if (channels > 1) { projImage = new CompositeImage(projImage, 0); ((CompositeImage) projImage).copyLuts(imp); if (method == SUM_METHOD || method == SD_METHOD) ((CompositeImage) projImage).resetDisplayRanges(); } if (frames > 1) projImage.setOpenAsHyperStack(true); Overlay overlay = imp.getOverlay(); if (overlay != null) { startSlice = start; stopSlice = stop; if (imp.getType() == ImagePlus.COLOR_RGB) projImage.setOverlay(projectRGBHyperStackRois(overlay)); else projImage.setOverlay(projectHyperStackRois(overlay)); } IJ.showProgress(1, 1); }
void createNewStack(ImagePlus imp, ImageProcessor ip) { int nSlices = imp.getStackSize(); int w = imp.getWidth(), h = imp.getHeight(); ImagePlus imp2 = imp.createImagePlus(); Rectangle r = ip.getRoi(); boolean crop = r.width != imp.getWidth() || r.height != imp.getHeight(); ImageStack stack1 = imp.getStack(); ImageStack stack2 = new ImageStack(newWidth, newHeight); ImageProcessor ip1, ip2; int method = interpolationMethod; if (w == 1 || h == 1) method = ImageProcessor.NONE; for (int i = 1; i <= nSlices; i++) { IJ.showStatus("Scale: " + i + "/" + nSlices); ip1 = stack1.getProcessor(i); String label = stack1.getSliceLabel(i); if (crop) { ip1.setRoi(r); ip1 = ip1.crop(); } ip1.setInterpolationMethod(method); ip2 = ip1.resize(newWidth, newHeight, averageWhenDownsizing); if (ip2 != null) stack2.addSlice(label, ip2); IJ.showProgress(i, nSlices); } imp2.setStack(title, stack2); Calibration cal = imp2.getCalibration(); if (cal.scaled()) { cal.pixelWidth *= 1.0 / xscale; cal.pixelHeight *= 1.0 / yscale; } IJ.showProgress(1.0); int[] dim = imp.getDimensions(); imp2.setDimensions(dim[2], dim[3], dim[4]); if (imp.isComposite()) { imp2 = new CompositeImage(imp2, ((CompositeImage) imp).getMode()); ((CompositeImage) imp2).copyLuts(imp); } if (imp.isHyperStack()) imp2.setOpenAsHyperStack(true); if (newDepth > 0 && newDepth != oldDepth) imp2 = (new Resizer()).zScale(imp2, newDepth, interpolationMethod); if (imp2 != null) { imp2.show(); imp2.changes = true; } }
void createNewStack(ImagePlus imp, ImageProcessor ip) { Rectangle r = ip.getRoi(); boolean crop = r.width != imp.getWidth() || r.height != imp.getHeight(); int nSlices = imp.getStackSize(); ImageStack stack1 = imp.getStack(); ImageStack stack2 = new ImageStack(newWidth, newHeight); ImageProcessor ip1, ip2; boolean interp = interpolate; if (imp.getWidth() == 1 || imp.getHeight() == 1) interp = false; for (int i = 1; i <= nSlices; i++) { IJ.showStatus("Scale: " + i + "/" + nSlices); ip1 = stack1.getProcessor(i); String label = stack1.getSliceLabel(i); if (crop) { ip1.setRoi(r); ip1 = ip1.crop(); } ip1.setInterpolate(interp); ip2 = ip1.resize(newWidth, newHeight); if (ip2 != null) stack2.addSlice(label, ip2); IJ.showProgress(i, nSlices); } ImagePlus imp2 = imp.createImagePlus(); imp2.setStack(title, stack2); Calibration cal = imp2.getCalibration(); if (cal.scaled()) { cal.pixelWidth *= 1.0 / xscale; cal.pixelHeight *= 1.0 / yscale; } int[] dim = imp.getDimensions(); imp2.setDimensions(dim[2], dim[3], dim[4]); IJ.showProgress(1.0); if (imp.isComposite()) { imp2 = new CompositeImage(imp2, 0); ((CompositeImage) imp2).copyLuts(imp); } if (imp.isHyperStack()) imp2.setOpenAsHyperStack(true); imp2.show(); imp2.changes = true; }
public void run(String arg) { String[] labels = { "Masked_Chromosomes", "Unmixed_Image", "Spectral_Image(optional)", "Spectra(optional)" }; ImagePlus[] imps = jutils.selectImages(true, 4, labels); if (imps == null) { return; } if (imps[0] == null) { return; } float[] mask = (float[]) imps[0].getStack().getPixels(2); findblobs3 fb = new findblobs3(imps[0].getWidth(), imps[0].getHeight()); float[] objects = fb.dofindblobs(mask, 0.5f); WaitForUserDialog dg = new WaitForUserDialog( "Optional Input", "Place RoiManager Points on Chromosome Segments (if desired)"); dg.show(); if (!dg.escPressed()) { RoiManager rman = RoiManager.getInstance(); while (rman != null && rman.getCount() > 1) { Roi[] rois = rman.getRoisAsArray(); int[] ids = new int[rois.length]; for (int i = 0; i < rois.length; i++) { Rectangle r = rois[i].getBounds(); ids[i] = (int) objects[r.x + fb.width * r.y]; } objects = fb.link_objects(objects, ids); rman.reset(); dg = new WaitForUserDialog( "Optional Input", "Place More RoiManager Points on Chromosome Segments (if desired)"); dg.show(); if (dg.escPressed()) break; } } int[] areas = fb.get_areas(objects); int[] temprank = jsort.get_javasort_order(areas); int[] arearank = jsort.get_javasort_order(temprank); for (int i = 0; i < fb.nobjects; i++) { arearank[i] = fb.nobjects - arearank[i] - 1; } // if the spectra are available, get them float[][][] spectra = null; Object[] data = null; if (imps[1] != null && imps[2] != null && imps[3] != null) { ImageWindow iw = imps[3].getWindow(); if (iw.getClass().getName().equals("jguis.PlotWindow4")) { float[][] yvals = (float[][]) jutils.runPW4VoidMethod(iw, "getYValues"); data = jutils.stack2array(imps[2].getStack()); Object[] coef = jutils.stack2array(imps[1].getStack()); spectra = new float[fb.nobjects][2][]; for (int i = 0; i < fb.nobjects; i++) { spectra[i][0] = fb.get_object_spectrum(objects, (i + 1), data, "Sum"); spectra[i][1] = new float[yvals[0].length]; float[] tempcoef = fb.get_object_spectrum(objects, (i + 1), coef, "Sum"); for (int j = 0; j < yvals[0].length; j++) { for (int k = 0; k < 5; k++) { spectra[i][1][j] += tempcoef[k] * yvals[k][j]; } } } } } CompositeImage imp = (CompositeImage) imps[0]; imp.setPosition(1, 1, 1); LUT graylut = jutils.get_lut_for_color(Color.white); imp.setChannelColorModel(graylut); imp.setPosition(2, 1, 1); LUT redlut = jutils.get_lut_for_color(Color.red); imp.setChannelColorModel(redlut); imp.setPosition(1, 1, 1); imp.updateAndRepaintWindow(); SkyPanel_v3 sp = new SkyPanel_v3(); int skychan = 6; if (imps[1] != null) skychan = imps[1].getNChannels(); // assume that the sky image has 6 channels and that the second is the unknown green // shift the unknown green to the end ImagePlus skyimp = null; if (imps[1] != null) { Object[] skystack = jutils.stack2array(imps[1].getStack()); // Object[] // skystack2={skystack[0],skystack[2],skystack[3],skystack[4],skystack[5],skystack[1]}; Object[] skystack2 = null; if (skychan == 6) skystack2 = new Object[] {skystack[0], skystack[2], skystack[3], skystack[4], skystack[5]}; else skystack2 = new Object[] {skystack[0], skystack[1], skystack[2], skystack[3], skystack[4]}; skyimp = new ImagePlus( "rearranged", jutils.array2stack(skystack2, imps[1].getWidth(), imps[1].getHeight())); } int nch = 5; if (skyimp != null) nch = skyimp.getStack().getSize(); GenericDialog gd2 = new GenericDialog("Options"); gd2.addNumericField("Area Accuracy (percent)", 30, 0); for (int i = 0; i < nch; i++) { gd2.addNumericField("Ch_" + (i + 1) + "_Contr_Thresh", 0.35, 5, 15, null); } // gd2.addNumericField("Contribution Threshold",0.35,5,15,null); gd2.addCheckbox("Mouse?", false); gd2.addNumericField("Box_Width", 150, 0); gd2.addNumericField("Box_Height", 100, 0); gd2.showDialog(); if (gd2.wasCanceled()) { return; } sp.areathresh = (float) gd2.getNextNumber(); sp.objthresh2 = new float[nch]; for (int i = 0; i < nch; i++) sp.objthresh2[i] = (float) gd2.getNextNumber(); // sp.objthresh=(float)gd2.getNextNumber(); boolean mouse = gd2.getNextBoolean(); int bwidth = (int) gd2.getNextNumber(); int bheight = (int) gd2.getNextNumber(); int[] colorindices = {4, 1, 2, 6, 3}; GenericDialog gd3 = new GenericDialog("Color Options"); for (int i = 0; i < 5; i++) gd3.addChoice( "Ch" + (i + 1) + " Color", SkyPanel_v3.colornames, SkyPanel_v3.colornames[colorindices[i]]); gd3.showDialog(); if (gd3.wasCanceled()) return; for (int i = 0; i < 5; i++) colorindices[i] = gd3.getNextChoiceIndex(); sp.colorindices = colorindices; sp.nch = 5; sp.dapilast = false; sp.cellwidth = bwidth; sp.cellheight = bheight; sp.init(imps[0], skyimp, objects, areas, arearank, fb, true, spectra, data, mouse); SkyPanel_v3.launch_frame(sp); }
/** * Invoked when an action occur. This causes the GUI to grab all input and to start calculation. */ private void launchCalculation() { // This method is called in the context of the event dispatch thread // Check inputs boolean is_valid = checkValid(); if (!is_valid) return; // Collect input from GUI widgets while under the event dispatch thread final Map<String, ImagePlus> imp_map = getImageMap(); final String expression = getExpression(); // Fork a new process to carry on the bulk of the execution, // freeing the event dispatch thread for other tasks like repainting windows // and dispatching other events: new Thread() { { // Reduce, at construction time, the priority of this Thread from ~15 // (that of the parent Thread, the AWT-EventQueue-0, aka Event Dispatch Thread) // to a more suitable one that doesn't compete with event dispatch: setPriority(Thread.NORM_PRIORITY); } public void run() { // Lock the GUI IJ.showStatus("IEP parsing...."); setGUIEnabled(false); try { if (null == image_expression_parser) { image_expression_parser = new Image_Expression_Parser<T>(); } // Prepare parser image_expression_parser.setExpression(expression); Image<T> result_img = null; // Here, we check if we get a RGB image. They are handled in a special way: // They are converted to 3 8-bit images which are processed separately, and // assembled back after processing. boolean is_rgb_image = false; for (String key : imp_map.keySet()) { if (imp_map.get(key).getType() == ImagePlus.COLOR_RGB) { is_rgb_image = true; } } if (is_rgb_image) { // Prepare holders Map<String, ImagePlus> red_map = new HashMap<String, ImagePlus>(); Map<String, ImagePlus> green_map = new HashMap<String, ImagePlus>(); Map<String, ImagePlus> blue_map = new HashMap<String, ImagePlus>(); ArrayList<Map<String, ImagePlus>> map_array = new ArrayList<Map<String, ImagePlus>>(3); map_array.add(red_map); map_array.add(green_map); map_array.add(blue_map); // Split stacks RGBStackSplitter channel_splitter = new RGBStackSplitter(); ImagePlus current_imp = null; for (String key : imp_map.keySet()) { current_imp = imp_map.get(key); // And stored individual channels in a new map channel_splitter.split(current_imp.getImageStack(), true); red_map.put( key, new ImagePlus(current_imp.getShortTitle() + "-R", channel_splitter.red)); green_map.put( key, new ImagePlus(current_imp.getShortTitle() + "-G", channel_splitter.green)); blue_map.put( key, new ImagePlus(current_imp.getShortTitle() + "-B", channel_splitter.blue)); } // Have the parser process individual channel separately Map<String, Image<T>> img_map; ImagePlus[] result_array = new ImagePlus[3]; Image<T> tmp_image; int index = 0; for (Map<String, ImagePlus> current_map : map_array) { img_map = image_expression_parser.convertToImglib(current_map); image_expression_parser.setImageMap(img_map); image_expression_parser.process(); // Collect results tmp_image = image_expression_parser.getResult(); result_array[index] = ImageJFunctions.copyToImagePlus(tmp_image); index++; } // Merge back channels RGBStackMerge rgb_merger = new RGBStackMerge(); ImagePlus new_imp = rgb_merger.mergeHyperstacks(result_array, false); // Jump through hoops... for (int channel = new_imp.getNChannels(); channel > 0; channel--) { new_imp.setPosition(channel, new_imp.getSlice(), new_imp.getFrame()); new_imp.resetDisplayRange(); } if (target_imp == null) { target_imp = new_imp; target_imp.show(); } else { if (!target_imp.isVisible()) { target_imp = new_imp; target_imp.show(); } else { target_imp.setStack(expression, new_imp.getStack()); if (target_imp.isComposite()) // Workaround: setStack() does not update CompositeImage's buffered image ((CompositeImage) target_imp).reset(); } } } else { Map<String, Image<T>> img_map = image_expression_parser.convertToImglib(imp_map); image_expression_parser.setImageMap(img_map); // Call calculation if (!image_expression_parser.process()) { IJ.error(image_expression_parser.getErrorMessage()); return; } // Collect result result_img = image_expression_parser.getResult(); if (target_imp == null) { target_imp = ImageJFunctions.copyToImagePlus(result_img); target_imp.show(); } else { ImagePlus new_imp = ImageJFunctions.copyToImagePlus(result_img); if (!target_imp.isVisible()) { target_imp = new_imp; target_imp.show(); } else { target_imp.setStack(expression, new_imp.getStack()); } } } target_imp.resetDisplayRange(); target_imp.updateAndDraw(); } catch (Exception e) { e.printStackTrace(); IJ.error("An error occurred: " + e); } finally { // Re-enable the GUI IJ.showStatus(""); setGUIEnabled(true); } } }.start(); }
public void itemStateChanged(ItemEvent e) { ImagePlus imp = WindowManager.getCurrentImage(); if (imp == null) return; if (!imp.isComposite()) { int channels = imp.getNChannels(); if (channels == 1 && imp.getStackSize() <= 4) channels = imp.getStackSize(); if (imp.getBitDepth() == 24 || (channels > 1 && channels < CompositeImage.MAX_CHANNELS)) { GenericDialog gd = new GenericDialog(imp.getTitle()); gd.addMessage("Convert to multi-channel composite image?"); gd.showDialog(); if (gd.wasCanceled()) return; else IJ.doCommand("Make Composite"); } else { IJ.error( "Channels", "A composite image is required (e.g., " + moreLabel + " Open HeLa Cells),\nor create one using " + moreLabel + " Make Composite."); return; } } if (!imp.isComposite()) return; CompositeImage ci = (CompositeImage) imp; Object source = e.getSource(); if (source == choice) { int index = ((Choice) source).getSelectedIndex(); switch (index) { case 0: ci.setMode(IJ.COMPOSITE); break; case 1: ci.setMode(IJ.COLOR); break; case 2: ci.setMode(IJ.GRAYSCALE); break; } ci.updateAndDraw(); if (Recorder.record) { String mode = null; switch (index) { case 0: mode = "composite"; break; case 1: mode = "color"; break; case 2: mode = "grayscale"; break; } Recorder.record("Stack.setDisplayMode", mode); } } else if (source instanceof Checkbox) { for (int i = 0; i < checkbox.length; i++) { Checkbox cb = (Checkbox) source; if (cb == checkbox[i]) { if (ci.getMode() == IJ.COMPOSITE) { boolean[] active = ci.getActiveChannels(); active[i] = cb.getState(); if (Recorder.record) { String str = ""; for (int c = 0; c < ci.getNChannels(); c++) str += active[c] ? "1" : "0"; Recorder.record("Stack.setActiveChannels", str); Recorder.record("//Stack.toggleChannel", imp.getChannel()); } } else { imp.setPosition(i + 1, imp.getSlice(), imp.getFrame()); if (Recorder.record) Recorder.record("Stack.setChannel", i + 1); } ci.updateAndDraw(); return; } } } }