getPixels

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

getPixels

Don Anderson-3
Hi,
I am new to Java and imageJ.
I want to modify the below code so i can access the pixel values. I put in
the following line:

byte[] pixels = (byte[])ip.getPixels();
(it is down in the section void macro1(ImagePlus imp, ImageProcessor ip)

but it does not work.
Can anyone help???
thank you


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import ij.plugin.frame.*;
import ij.*;
import ij.process.*;
import ij.gui.*;

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

import ij.*;
import ij.plugin.PlugIn;

/**
        Image Processing Demo. Demonstrates how to create a custom user
interface, how
        to use ImageJ's ImageProcessor class, and how to run commands in a
separate thread.
*/
public class sample_ extends PlugInFrame implements ActionListener {

        Panel panel;
        int previousID;
        static Frame instance;



        public sample_() {
                super("IP Demo");
                if (instance!=null) {
                        instance.toFront();
                        return;


                }
                instance = this;
                IJ.register(sample_.class);

                setLayout(new FlowLayout());
                panel = new Panel();
                panel.setLayout(new GridLayout(4, 4, 5, 5));
                addButton("Reset");
                addButton("Flip");
                addButton("Invert");
                addButton("Rotate");
                addButton("Lighten");
                addButton("Darken");
                addButton("Zoom In");
                addButton("Zoom Out");
                addButton("Smooth");
                addButton("Sharpen");
                addButton("Find Edges");
                addButton("Threshold");
                addButton("Add Noise");
                addButton("Reduce Noise");
                addButton("Macro1");
                addButton("Macro2");
                add(panel);
               
                pack();
                GUI.center(this);
                show();
        }
       
        void addButton(String label) {
                Button b = new Button(label);
                b.addActionListener(this);
                panel.add(b);
        }

        public void actionPerformed(ActionEvent e) {
                ImagePlus imp = WindowManager.getCurrentImage();
                if (imp==null) {
                        IJ.beep();
                        IJ.showStatus("No image");
                        previousID = 0;
                        return;
                }
                if (!imp.lock())
                        {previousID = 0; return;}
                ImageProcessor ip = imp.getProcessor();

                int id = imp.getID();
                if (id!=previousID)
                        ip.snapshot();
                previousID = id;
                String label = e.getActionCommand();
                if (label==null)
                        return;
                new IPDemoRunner(label, imp, ip);
        }

        public void processWindowEvent(WindowEvent e) {
                super.processWindowEvent(e);
                if (e.getID()==WindowEvent.WINDOW_CLOSING) {
                        instance = null;
                }
        }

}

class IPDemoRunner extends Thread {
        private String command;
        private ImagePlus imp;
        private ImageProcessor ip;

        IPDemoRunner(String command, ImagePlus imp, ImageProcessor ip) {
                super(command);
                this.command = command;
                this.imp = imp;
                this.ip = ip;
                setPriority(Math.max(getPriority()-2, MIN_PRIORITY));
                start();
        }

        public void run() {
                try {runCommand(command, imp, ip);}
                catch(OutOfMemoryError e) {
                        IJ.outOfMemory(command);
                        if (imp!=null) imp.unlock();
                } catch(Exception e) {
                        CharArrayWriter caw = new CharArrayWriter();
                        PrintWriter pw = new PrintWriter(caw);
                        e.printStackTrace(pw);
                        IJ.write(caw.toString());
                        IJ.showStatus("");
                        if (imp!=null) imp.unlock();
                }
        }

        void runCommand(String command, ImagePlus imp, ImageProcessor ip) {
                IJ.showStatus(command + "...");
                Roi roi = imp.getRoi();
                if (command.startsWith("Zoom")||command.startsWith("Macro")
||command.equals("Threshold"))
                        {roi = null; ip.resetRoi();}
                ImageProcessor mask =  roi!=null?roi.getMask():null;
                if (command.equals("Reset"))
                        ip.reset();
                else if (command.equals("Flip"))
                        ip.flipVertical();

                else if (command.equals("Invert"))
                        ip.invert();
                else if (command.equals("Lighten")) {
                        if (imp.isInvertedLut())
                                ip.multiply(0.9);
                        else
                                ip.multiply(1.1);
                }
                else if (command.equals("Darken")) {
                        if (imp.isInvertedLut())
                                ip.multiply(1.1);
                        else
                                ip.multiply(0.9);
                }
                else if (command.equals("Rotate"))
                        ip.rotate(30);
                else if (command.equals("Zoom In"))
                        ip.scale(1.2, 1.2);
                else if (command.equals("Zoom Out"))
                        ip.scale(.8, .8);
                else if (command.equals("Threshold"))
                        ip.autoThreshold();
                else if (command.equals("Smooth"))
                        ip.smooth();
                else if (command.equals("Sharpen"))
                        ip.sharpen();
                else if (command.equals("Find Edges"))
                        ip.findEdges();
                else if (command.equals("Add Noise"))
                        ip.noise(20);
                else if (command.equals("Reduce Noise"))
                        ip.medianFilter();
                else if (command.equals("Macro1"))
                        macro1(imp, ip);
                else if (command.equals("Macro2"))
                        macro2(imp, ip);
                if (mask!=null) ip.reset(mask);
                imp.updateAndDraw();
                imp.unlock();
                IJ.showStatus("");
        }

        void macro1(ImagePlus imp, ImageProcessor ip) {
               
int volumeCount = ip.getWidth();


byte[] pixels = (byte[])ip.getPixels();


IJ.write("the width of this image is: " + volumeCount );

               
        }
       
        void macro2(ImagePlus imp, ImageProcessor ip) {
                double scale = 1, m = 1.2;
                for (int i=0; i<20; i++) {
                        ip.reset();
                        scale *= m;
                        ip.scale(scale, scale);
                        imp.updateAndDraw();
                        IJ.wait(10);
                }
                for (int i=0; i <20; i++) {
                        ip.reset();
                        scale /= m;
                        ip.scale(scale, scale);
                        imp.updateAndDraw();
                        IJ.wait(10);
                }
        }

}
Reply | Threaded
Open this post in threaded view
|

Re: getPixels

dscho
Hi,

On Fri, 16 Feb 2007, Don Anderson wrote:

> I want to modify the below code so i can access the pixel values. I put in
> the following line:
>
> byte[] pixels = (byte[])ip.getPixels();
> (it is down in the section void macro1(ImagePlus imp, ImageProcessor ip)

Please, do not send tons of code which are totally uninteresting to your
problem. It is annoying.

Then, you did not say what exactly "does not work". If you don't see
changed, you might want to try the method updateAndDraw() of ImagePlus.
Withouth such a notification, the image is not redrawn.

BTW, just because I saw the code anyway:


> if (instance!=null) {
> instance.toFront();
> return;

This, inside a constructor, is bad style. Yes, you find it literally
everywhere, but the problem is that you can create as many objects of this
class as you want, but only the first will work!

(For a nice example, try to instantiate the ROIManager() twice. It will
compile without complaining, the second instance will get created without
complaining, but _using_ the second instance will throw NullPointer
exceptions.)

The method to ensure that only one instance of the class may exist (which
is called a "singleton"), is better done like this:

        protected MyClass() { ... } // the constructor is protected
        protected MyClass instance;
        public static MyClass getInstance() {
                if (instance == null)
                        instance = new MyClass();
                return instance;
        }

Of course, the constructor of a plugin must not be less than public, which
means that the plugin itself cannot be the singleton! (Just look at it for
a moment: a plugin is not meant to be called just once...)

In other words, if you need such a singleton, you should make a separate
class like described above, and get the instance in the constructor of the
plugin.

Ciao,
Dscho
Reply | Threaded
Open this post in threaded view
|

Re: getPixels

Don Anderson-3
In reply to this post by Don Anderson-3
Ok ,how about the simple example below. I still get an error when using
int[] pixels = (int[])ip.getPixels();


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

public class sample_ implements PlugIn {

        public void run(String arg) {
                ImagePlus imp = WindowManager.getCurrentImage();
               
               
                ImageProcessor ip = imp.getProcessor();
               
                int[] pixels = (int[])ip.getPixels();
               
               
IJ.write("the pixel value is: " + pixels[1]);
         }

}
Reply | Threaded
Open this post in threaded view
|

Re: getPixels

Albert Cardona
> Ok ,how about the simple example below. I still get an error when using
> int[] pixels = (int[])ip.getPixels();
>  

ImageProcessor.getPixels() returns an Object, generic, for a reason. Are
you providing an RGB image? Otherwise the cast will fail.

Albert