Add slices to a stack in a loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Add slices to a stack in a loop

Abderahmane Habaieb
Hello all,

I have problems to add slice to my stack in my plugin. The goal is to have a loop where images are loaded and added to an image. In clear, the image would be updated at each loop and user could go through the slices.

I tried something like :

ImagePlus imp = IJ.createImage("title", 500, 500, 0, 24);
imp.show();
for(int i = 0; i<5; i++) {
     raw = IJ.openImage(ith_Image);
     ImageStack stack = this.imp.getStack();
     stack.addSlice("", raw.getProcessor().getPixelsCopy());
}

but the image imp is not actually updated and stay white until the end of the for loop. Plus, I also tried with Concatenator class but problem remains the same.

Is there a function like update or something like that ?

Thanks in advance for your help.

Abdou
Reply | Threaded
Open this post in threaded view
|

Re: Add slices to a stack in a loop

Rasband, Wayne (NIH/NIMH) [E]
On Aug 11, 2014, at 10:50 AM, Abderahmane Habaieb wrote:

> Hello all,
>
> I have problems to add slice to my stack in my plugin. The goal is to have a
> loop where images are loaded and added to an image. In clear, the image
> would be updated at each loop and user could go through the slices.

Use the ImagePlus.setStack() method to update the image. The following is a JavaScript example.

-wayne

  imp = createImage(1);
  imp.show();
  stack = imp.getStack();
  for (i=2; i<=100; i++) {
    raw = createImage(i);
    stack.addSlice(raw.getProcessor());
    imp.setStack(stack);
    imp.setSlice(stack.getSize());
    IJ.wait(50);
  }

  function createImage(n) {
     var imp = IJ.createImage("Untitled", "8-bit black", 500, 500, 1);
     var ip = imp.getProcessor();
     var font = new Font("SansSerif", Font.PLAIN, 200);
     ip.setFont(font);
     ip.setColor(Color.white);
     ip.drawString(""+n, 100, 300);
     return imp;
  }


> I tried something like :
>
> ImagePlus imp = IJ.createImage("title", 500, 500, 0, 24);
> imp.show();
> for(int i = 0; i<5; i++) {
>     raw = IJ.openImage(ith_Image);
>     ImageStack stack = this.imp.getStack();
>     stack.addSlice("", raw.getProcessor().getPixelsCopy());
> }
>
> but the image imp is not actually updated and stay white until the end of
> the for loop. Plus, I also tried with Concatenator class but problem remains
> the same.
>
> Is there a function like update or something like that ?
>
> Thanks in advance for your help.
>
> Abdou
>
>
>
> --
> View this message in context: http://imagej.1557.x6.nabble.com/Add-slices-to-a-stack-in-a-loop-tp5009094.html
> Sent from the ImageJ mailing list archive at Nabble.com.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Add slices to a stack in a loop

Abderahmane Habaieb
Thank you Wayne for your quick answer.
Unfortunately, it hasn't solved the problem. I can see the scrollbar of the frame reduce and become smaller, but no slice is printed and the slice remains white until the end of the for-loop. I tested several wait delay, but it didn't have any effect. Do you have idea about it?

                ImagePlus raw;
                this.impRaw = createImage(1);
                this.impRaw.show();
                ImageStack stack = this.impRaw.getStack();
               
                for (int i = 2; i <= 200; i++) {
                        raw = createImage(i);
                        stack.addSlice(raw.getProcessor());
                        //stack.update(raw.getProcessor());
                        this.impRaw.setStack(stack);
                        this.impRaw.setSlice(stack.getSize());
                        IJ.wait(50);
                        //IJ.wait(500);
                        //IJ.wait(5);
                 }

Thank you very much,
Abdou

P.S : Sorry for the grammar mistakes if there are
Reply | Threaded
Open this post in threaded view
|

Re: Add slices to a stack in a loop

Abderahmane Habaieb
Well, I tried different things and here is what I found :

For this code :

import ij.plugin.PlugIn;
import ij.*;
import ij.process.*;
import java.awt.*;

/**
 * This is a template for a plugin that does not require one image
 * (be it that it does not require any, or that it lets the user
 * choose more than one image in a dialog).
 */
public class App implements PlugIn {

        @Override
        public void run(String arg) {
                ImagePlus raw;
                ImagePlus impRaw = createImage(1);
                impRaw.show();
                ImageStack stack = impRaw.getStack();
                       
                for (int i = 2; i <= 200; i++) {
                        raw = createImage(i);
                  stack.addSlice(raw.getProcessor());
                  //stack.update(raw.getProcessor());
                  impRaw.setStack(stack);
                        impRaw.setSlice(stack.getSize());
                        IJ.wait(50);
                        //IJ.wait(500);
                        //IJ.wait(5);
                }
        }

        private ImagePlus createImage(int n) {
                ImagePlus imp = IJ.createImage("Untitled", "8-bit black", 500, 500, 1);
                ImageProcessor ip = imp.getProcessor();
                Font font = new Font("SansSerif", Font.PLAIN, 200);
                ip.setFont(font);
                ip.setColor(Color.white);
                ip.drawString(""+n, 100, 300);
                return imp;
    }
}

It works perfectly, that's what I want !
But for this code which have a window :

import ij.plugin.PlugIn;
import ij.*;
import ij.plugin.frame.PlugInFrame;
import ij.process.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * This is a template for a plugin that does not require one image (be it that
 * it does not require any, or that it lets the user choose more than one image
 * in a dialog).
 */
public class App implements PlugIn {

    @Override
    public void run(String arg) {
        PlugInFrame frame = new PlugInFrame("LOL");
        JPanel panel = new JPanel();
        JButton b = new JButton("Hey !");
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ImagePlus raw;
                ImagePlus impRaw = createImage(1);
                impRaw.show();
                ImageStack stack = impRaw.getStack();
                       
                for (int i = 2; i <= 200; i++) {
                        raw = createImage(i);
                  stack.addSlice(raw.getProcessor());
                  //stack.update(raw.getProcessor());
                  impRaw.setStack(stack);
                        impRaw.setSlice(stack.getSize());
                        IJ.wait(50);
                        //IJ.wait(500);
                        //IJ.wait(5);
                }
            }
        });
        panel.add(b);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    private ImagePlus createImage(int n) {
        ImagePlus imp = IJ.createImage("Untitled", "8-bit black", 500, 500, 1);
        ImageProcessor ip = imp.getProcessor();
        Font font = new Font("SansSerif", Font.PLAIN, 200);
        ip.setFont(font);
        ip.setColor(Color.white);
        ip.drawString("" + n, 100, 300);
        return imp;
    }
}

It reproduces the same problem, the image remains white until end of loop.


I hope it will help you to have an idea about this issue because, me, I really don't have any one hahaha.
Thanks in advance,
Abdou
Reply | Threaded
Open this post in threaded view
|

Re: Add slices to a stack in a loop

Abderahmane Habaieb
Rasband, Wayne (NIH/NIMH) wrote
Dear Abdou,

The display will not be updated until the end of the loop if the loop runs on the event dispatch thread. You need to spawn a separate thread that runs the loop.

Best regards,
Yay thank you it works !! I created an independant thread and it's all rolling along :D