ImageJ adicionar duas linha no mesmo Plot

0 respostas
JavaX_JavaX

Boa tarde!

pessoal estou usando a Api ImageJ, para processamento de imagens

e não estou conseguindo achar um meio de criar um plot com duas linhas de medidas.

uma para ler na horizontal e outra na vertical da imagem

bom segue abaixo uma parte de código

se alguém puder ajudar

//MouseListener, MouseMotionListener, KeyListener: to detect changes to the selection of an ImagePlus
    //ImageListener: listens to changes (updateAndDraw) and closing of an image
    //Runnable: for background thread
    private ImagePlus imp;                  //the ImagePlus that we listen to and the last one
    private ImagePlus plotImage;            //where we plot the profile
    private Thread bgThread;                //thread for plotting (in the background)
    private boolean doUpdate;               //tells the background thread to update

    /* Initialization and plot for the first time. Later on, updates are triggered by the listeners **/
    public void run(String arg) {
        imp = WindowManager.getCurrentImage();
        if (imp==null) {
            IJ.noImage(); return;
        }

        if (!isSelection()) {
           IJ.error("Dynamic Profiler","Line or Rectangular Selection Required"); return;
        }
        ImageProcessor ip = getProfilePlot();  // get a profile
        if (ip==null) {                     // no profile?
            IJ.error("Dynamic Profiler","No Profile Obtained"); return;
        }
                                            // new plot window
        plotImage = new ImagePlus("Profile of "+imp.getShortTitle(), ip);
        plotImage.show();
        IJ.wait(50);
        positionPlotWindow();
                                            // thread for plotting in the background

        bgThread = new Thread(this, "Dynamic Profiler Plot");
        bgThread.setPriority(Math.max(bgThread.getPriority()-3, Thread.MIN_PRIORITY));
        bgThread.start();
        createListeners();
    }

    // these listeners are activated if the selection is changed in the corresponding ImagePlus
    public synchronized void mousePressed(MouseEvent e) { doUpdate = true; notify(); }   
    public synchronized void mouseDragged(MouseEvent e) { doUpdate = true; notify(); }
    public synchronized void mouseClicked(MouseEvent e) { doUpdate = true; notify(); }
    public synchronized void keyPressed(KeyEvent e) { doUpdate = true; notify(); }
    // unused listeners concering actions in the corresponding ImagePlus
    public void mouseReleased(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseMoved(MouseEvent e) {}
    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void imageOpened(ImagePlus imp) {}

    // this listener is activated if the image content is changed (by imp.updateAndDraw)
    public synchronized void imageUpdated(ImagePlus imp) {
        if (imp == this.imp) { 
            if (!isSelection())
                IJ.run(imp, "Restore Selection", "");
            doUpdate = true;
            notify();
        }
    }

    // if either the plot image or the image we are listening to is closed, exit
    public void imageClosed(ImagePlus imp) {
        if (imp == this.imp || imp == plotImage) {
            removeListeners();
            closePlotImage();                       //also terminates the background thread
        }
    }

    // the background thread for plotting.
    public void run() {
        while (true) {
            IJ.wait(50);                            //delay to make sure the roi has been updated
            ImageProcessor ip = getProfilePlot();
            if (ip != null) plotImage.setProcessor(null, ip);
            synchronized(this) {
                if (doUpdate) {
                    doUpdate = false;               //and loop again
                } else {
                    try {wait();}                   //notify wakes up the thread
                    catch(InterruptedException e) { //interrupted tells the thread to exit
                        return;
                    }
                }
            }
        }
    }

    private synchronized void closePlotImage() {    //close the plot window and terminate the background thread
        bgThread.interrupt();
        plotImage.getWindow().close();
    }

    private void createListeners() {
        ImageWindow win = imp.getWindow();
        ImageCanvas canvas = win.getCanvas();
        canvas.addMouseListener(this);
        canvas.addMouseMotionListener(this);
        canvas.addKeyListener(this);
        imp.addImageListener(this);
        plotImage.addImageListener(this);
    }

    private void removeListeners() {
        ImageWindow win = imp.getWindow();
        ImageCanvas canvas = win.getCanvas();
        canvas.removeMouseListener(this);
        canvas.removeMouseMotionListener(this);
        canvas.removeKeyListener(this);
        imp.removeImageListener(this);
        plotImage.removeImageListener(this);
    }

    /** Place the plot window to the right of the image window */
    void positionPlotWindow() {
        IJ.wait(500);
        if (plotImage==null || imp==null) return;
        ImageWindow pwin = plotImage.getWindow();
        ImageWindow iwin = imp.getWindow();
        if (pwin==null || iwin==null) return;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension plotSize = pwin.getSize();
        Dimension imageSize = iwin.getSize();
        if (plotSize.width==0 || imageSize.width==0) return;
        Point imageLoc = iwin.getLocation();
        int x = imageLoc.x+imageSize.width+10;
        if (x+plotSize.width>screen.width)
            x = screen.width-plotSize.width;
        pwin.setLocation(x, imageLoc.y);
        ImageCanvas canvas = iwin.getCanvas();
        canvas.requestFocus();
    }

    /** get a profile, analyze it and return a plot (or null if not possible) */
    ImageProcessor getProfilePlot() {
        if (!isSelection()) return null;
        ImageProcessor ip = imp.getProcessor();
        Roi roi = imp.getRoi();
        roi = new ShapeRoi(roi);
	ShapeRoi roiShape = (ShapeRoi) roi;
        if (ip == null || roi == null) return null; //these may change asynchronously
        if (roi.getType() == Roi.LINE)
            ip.setInterpolate(PlotWindow.interpolate); 
        else
            ip.setInterpolate(false);
        ProfilePlot profileP = new ProfilePlot(imp, Prefs.verticalProfile);//get the profile
        if (profileP == null) return null;
        double[] profile = profileP.getProfile();
        if (profile==null || profile.length<2)
            return null;
        String xUnit = "pixels";                    //the following code is mainly for x calibration
        double xInc = 1;
        Calibration cal = imp.getCalibration();
        if (roi.getType() == Roi.LINE) {
            Line line = (Line)roi;
            if (cal != null) {
                //System.out.println("Passou Aqui 1 !!!!");
                double dx = cal.pixelWidth*(line.x2 - line.x1);
                double dy = cal.pixelHeight*(line.y2 - line.y1);
                double length = Math.sqrt(dx*dx + dy*dy);
                xInc = length/(profile.length-1);
                xUnit = cal.getUnits();
            }
        } else if (roi.getType() == Roi.RECTANGLE || roiShape.getType() == ShapeRoi.COMPOSITE ) {
            if (cal != null) {
                //System.out.println("Passou Aqui 2 !!!!");
                //xInc   = roi.getBounds().getWidth()*cal.pixelWidth/(profile.length-1);
                xInc   = roi.getBounds().getWidth()*cal.pixelWidth/(profile.length-1);
                System.out.println(xInc);
                xUnit  = cal.getUnits();
            }
        } else return null;
        String xLabel = "Distance (" + xUnit + ")";
        String yLabel = (cal !=null && cal.getValueUnit()!=null && !cal.getValueUnit().equals("Gray Value")) ?
            "Value ("+cal.getValueUnit()+")" : "Value";

        
        //********************************************** Comeco Original funcionando  
        
        /*
        int n = profile.length;  
        // create the x axis
        double[] x = new double[n];
        for (int i=0; i<n; i++)
            x[i] = i*xInc;
        
        Plot plot = new Plot("The window title", xLabel, yLabel, x, profile);

        plot.setLineWidth (2);

        if (profile.length == 251){
          plot.setColor(Color.red);
        }else{
          plot.setColor(Color.blue);
        }
       
        plot.addPoints(x, profile, Plot.LINE);
        
         double ymin = ProfilePlot.getFixedMin();
         double ymax= ProfilePlot.getFixedMax();
	  if (!(ymin==0.0 && ymax==0.0)) {
	    double[] a = Tools.getMinMax(x);
	    double xmin=a[0]; double xmax=a[1];
	    plot.setLimits(xmin, xmax, ymin, ymax);
	 }*/

        //********************************************** Final Original funcionando  

          
          
        //******************************************* Comeco Teste
          
        int n = profile.length;  
        // create the x axis
        double[] x = new double[n];
        for (int i=0; i<n; i++)
            x[i] = i*xInc;
        
                double[] x1 = x;
                double[] x2 = profile;
                double[] y1 = profile;
                double[] y2 = x;

                
             
            
                // Initialize the plot with x/y
                //Plot plot = new Plot("Example plot", "x", "y", x, profile);
               //Plot plot = new Plot("The window title", xLabel, yLabel, x1, y1);
               Plot plot = new Plot("The window title", xLabel, yLabel, x1, y1);

                
                // Largura da Linha
                plot.setLineWidth (2);

           double ymin = ProfilePlot.getFixedMin();
           double ymax= ProfilePlot.getFixedMax();
	
           
           if (!(ymin==0.0 && ymax==0.0)) {
		double[] a = Tools.getMinMax(x);
		double xmin=a[0]; double xmax=a[1];
		plot.setLimits(xmin, xmax, ymin, ymax);
	   }
           
           
           
                // Add x/y2 in blue; need to draw the previous data first
                plot.draw();
                plot.setColor(Color.blue);
                plot.addPoints(x1, x2, Plot.LINE);

                
                
                // Add x3/y3 as circles instead of connected lines
                

                System.out.print(profile.toString());

                plot.draw();
                plot.setColor(Color.red);
                plot.addPoints(y1, y2, Plot.LINE);
            
        
        
        
        
                
        
        
        
        
        
        
              
       //******************************************* Final Teste
        
        
        return plot.getProcessor();

att JavaX

Criado 9 de abril de 2014
Respostas 0
Participantes 1