Not returning a new Image

I’m doing a college project and creating a plugin GUI for imageJ. I’ve recieved help before off this forum. I’m using already made Image J plugins and trying to call the classes from my GUI. Each button on my GUI will preform an operation on the image. I’m having trouble calling methods from the other files, syntactically I think I’m right but my image does not seem to be updating in my GUI. Its like I’m not performing the action. I’m using the FFT_ java class from https://imagej.nih.gov/ij/plugins/download/FFT_.java and my own code is

import ij.plugin.*;
import ij.*;
import ij.gui.*;
import ij.process.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class my_tester implements PlugIn
 {    
static int plotWidth =400;
static double angleInDegrees =  40;
static double angle = (angleInDegrees/360.0)*2.0*Math.PI;
static int polygonMultiplier = 100;
static boolean oneToOne;
double picsize;
    
    ImagePlus img, img2;
    int[] x,y;


    static final int WIDTH = 800;
    static final int HEIGHT = 600;

    public void run(String arg) {
        img = IJ.openImage();//this open as image dialog box and saves as img
        //img = WindowManager.getCurrentImage();//this takes an existing image, saves as
        if (img==null) {
            ImageProcessor ip = new ByteProcessor(WIDTH, HEIGHT);
            IJ.log("error at 34");
            ip.setColor(Color.red);
            ip.fill();//fills a red panel if no image is selected
            img = new ImagePlus("Panel Window", ip);
        }
        CustomCanvas cc = new CustomCanvas(img);//create my canvas to plant the selected Image on
        if (img.getStackSize()>1)
            new CustomStackWindow(img, cc);
        else
           new CustomWindow(img, cc);
        cc.requestFocus();
    }
    /*//JUST AN EXAMPLE DELETE ME
    public class mise extends Interactive_3D_Surface_Plot{

        /**
         *
         * @param imp
         * @param targetWidth
         * @param targetHeight
         //
        //@Override
        public void cropAndResize(){
            
        }
    }
    */
    class CustomCanvas extends ImageCanvas {
    
        CustomCanvas(ImagePlus img) {//constructor for my Image Canvas
            super(img);
        }
    
        public void mousePressed(MouseEvent e) {//gives the co-ord of the desired pixel
            super.mousePressed(e);
            IJ.log("This pixels co-ord is: \nX = ("+offScreenX(e.getX())+") and Y = ("+offScreenY(e.getY())+")");
        }
    
    } // CustomCanvas inner class
    
    
    class CustomWindow extends ImageWindow implements ActionListener {
    
        private Button button1, button2, button3, button4;
       
        CustomWindow(ImagePlus img, ImageCanvas ic) {//Constructor for my custom window, takes in my image and the canvas as arguments
            super(img, ic);
            addPanel();//add panel
        }
    
        void addPanel() {//add panel method
            Panel panel = new Panel();//construct a panel
            //panel.setLayout(new FlowLayout(FlowLayout.LEADING));//CHANGE LAYOUT IF AVAILABLE TIME, USE LAYOUT FROM IMAGE_J gui
            panel.setLayout(new GridBagLayout());//set layout
            GridBagConstraints c = new GridBagConstraints();//set constraints
            c.insets = new Insets(10, 10, 10, 10);//add border around buttons
            
            /* create new buttons */
            button1 = new Button(" FFT ");
            button2 = new Button(" InverseFFT ");
            button3 = new Button(" 3D Plot ");
            button4 = new Button(" Take crop ");
            
            /* add the action listeners */
            button1.addActionListener(this);
            button2.addActionListener(this);
            button3.addActionListener(this);
            button4.addActionListener(this);
            
            /* add the buttons to the panel and set its grid location*/
            panel.add(button1, c);
            c.gridx = 1;
            c.gridy = 0;
            panel.add(button2, c);
            c.gridx = 2;
            c.gridy = 0;
            panel.add(button3, c);
            c.gridx = 3;
            c.gridy = 0;
            panel.add(button4, c);
            c.gridx = 4;
            c.gridy = 0;
            add(panel, BorderLayout.EAST);//add the panel and layout
            pack();//pack into the same window so all buttons are visable
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            Point loc = getLocation();
            Dimension size = getSize();
            if (loc.y+size.height>screen.height)
                getCanvas().zoomOut(0, 0);
         }
      
        public void actionPerformed(ActionEvent e) //what happens when i press a button
        {
            Object name = e.getSource();
            if (name.equals(" FFT ")) 
            {
                FFT_ ffyt = new FFT_();//create instance
                ImageProcessor imp = img.getProcessor();
                img = ffyt.fft(imp,true);//calling the fft method from FFT_ on the object
                
                //img.show();
                //img = ffyt.run(imp);
                img.updateAndDraw();

            }
            else if (name.equals(" InverseFFT ")) 
            { 
                FFT_ infft = new FFT_();
                ImageProcessor imp = img.getProcessor();
                img = infft.fft(imp,true);//calling the fft method from FFT_ on the object
                //img = infft.run(imp);
                img.updateAndDraw();
            } 
            else if(name.equals(" 3D Plot ")) 
            { 
                //Interactive_3D_Surface_Plot ddd = new Interactive_3D_Surface_Plot();
                //Surface_Plotter ddd = new Surface_Plot();
                //ddd.run();
                img.getProcessor();
                //dddplot(img);//check this not right
                img.updateAndDraw();
            }
            else if(name.equals(" Take crop ")) 
            {
                Crop_Test crop = new Crop_Test();
                //ImagePlus imp = img.getProcessor();
                try {
                    crop.cropAndResize(img, plotWidth, HEIGHT);
                } catch (Exception ex) {
                    Logger.getLogger(my_tester.class.getName()).log(Level.SEVERE, null, ex);
                }
                //ImagePlus img = IJ.openImage();
                img.updateAndDraw();
                //cropAndResize();
            } 
            else 
            {
            }
            ImageCanvas ic = img.getCanvas();
            if (ic!=null)
                ic.requestFocus();
        }
        
    } // CustomWindow inner class


    class CustomStackWindow extends StackWindow implements ActionListener {
    
        private Button button1, button2, button3, button4;
       
        CustomStackWindow(ImagePlus img, ImageCanvas ic) {
            super(img, ic);
            addPanel();
       }
    
        void addPanel() {
            Panel panel = new Panel();//construct a panel
            //panel.setLayout(new FlowLayout(FlowLayout.LEADING));//CHANGE LAYOUT IF AVAILABLE TIME, USE LAYOUT FROM IMAGE_J gui
            panel.setLayout(new GridBagLayout());//set layout
            GridBagConstraints c = new GridBagConstraints();//set constraints
            c.insets = new Insets(10, 10, 10, 10);//add border around buttons
            
            /* create new buttons */
            button1 = new Button(" FFT ");
            button2 = new Button(" InverseFFT ");
            button3 = new Button(" 3D Plot ");
            button4 = new Button(" Take crop ");
            
            /* add the action listeners */
            button1.addActionListener(this);
            button2.addActionListener(this);
            button3.addActionListener(this);
            button4.addActionListener(this);
            
            /* add the buttons to the panel and set its grid location*/
            panel.add(button1, c);
            c.gridx = 0;
            c.gridy = 1;
            panel.add(button2, c);
            c.gridx = 0;
            c.gridy = 2;
            panel.add(button3, c);
            c.gridx = 0;
            c.gridy = 3;
            panel.add(button4, c);
            c.gridx = 0;
            c.gridy = 4;
            add(panel, BorderLayout.EAST);//add the panel and layout
            pack();//pack into the same window so all buttons are visable
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            Point loc = getLocation();
            Dimension size = getSize();
            if (loc.y+size.height>screen.height)
                getCanvas().zoomOut(0, 0);
         }
      
        public void actionPerformed(ActionEvent e) 
        {
            Object name = e.getSource();
            if (name.equals(" FFT ")) 
            {
                FFT_ ffyt = new FFT_();//create instance
                ImageProcessor imp = img.getProcessor();
                ffyt.fft(imp,true).show();//calling the fft method from FFT_ on the object
                //img.show();
                //img = ffyt.run(imp);
                //img.updateAndDraw();
            }
            else if (name.equals(" InverseFFT ")) 
            { 
                FFT_ infft = new FFT_();
                ImageProcessor imp = img.getProcessor();
                infft.fft(imp,true);//calling the fft method from FFT_ on the object
                //img = infft.run(imp);
                img.updateAndDraw();
            } 
            else if(name.equals(" 3D Plot ")) 
            { 
                //Interactive_3D_Surface_Plot ddd = new Interactive_3D_Surface_Plot();
                //Surface_Plotter ddd = new Surface_Plot();
                //ddd.run();
                img.getProcessor();
                //dddplot(img);//check this not right
                img.updateAndDraw();
            }
            else if(name.equals(" Take crop ")) 
            {
                Crop_Test crop = new Crop_Test();
                //ImagePlus imp = img.getProcessor();
                try {
                    crop.cropAndResize(img, plotWidth, HEIGHT);
                } catch (Exception ex) {
                    Logger.getLogger(my_tester.class.getName()).log(Level.SEVERE, null, ex);
                }
                //ImagePlus img = IJ.openImage();
                img.updateAndDraw();
                //cropAndResize();
            } 
            else 
            {
            }
            ImageCanvas ic = img.getCanvas();
            if (ic!=null)
                ic.requestFocus();
        }
        
      
    } // CustomStackWindow inner class
    public static void main(String args[])
    {
       new my_tester();
    }}

In the action performed function I have

     if (name.equals(" FFT ")) 
            {
                FFT_ ffyt = new FFT_();//create instance
                ImageProcessor imp = img.getProcessor();
                img = ffyt.fft(imp,true);//calling the fft method from FFT_ on the object
                
                //img.show();
                //img = ffyt.run(imp);
                img.updateAndDraw();

            }

Am I using this right, I’ve created an instance of the FFT_ class, got the image processor of the image plus, called the function fft(imp, true) on my instance and tried to update the image on my GUI, but it does not seem to work. Can anyone provide any insight to what I’m doing wrong or how to fix this.

From a fast look at your code, it looks like img is the image shown in your GUI, not img2. So you would have to set the result to the displayed image.

Iarganda yes, I changed it back to img, img was what i was using origionally, but it does not run when it is img either. From the FFT_ plugin am I calling the wrong method, I have tried the others but still no success

@brian_mitchell I guess the aim of your college project is to learn Java, so please have a look at the method signature you are calling in FFT_.java:

public void fft(ImageProcessor ip, boolean inverse)

void means it doesn’t return anything, so this:

… will return null.

As the FFT_ class calls new ImagePlus().show() at quite some places, it might not be the optimal choice for your project. I suggest to either change the code of this implementation, or use e.g. the ImgLib2 implementation.

1 Like

Hi, apologies, I should of mentioned I edited the FFT_ class, I added returns and removed the void from the method.

This is my FFT_ class. Ive changed the methods run() and fft() from the FFT_ class


        
	
	public ImagePlus run(ImageProcessor ip) {
		boolean inverse;
		if (!powerOf2Size(ip)) {
			IJ.error("A square, power of two size image or selection\n(128x128, 256x256, etc.) is required.");
			//return;
		}
		ImageProcessor fht  = (ImageProcessor)imp.getProperty("FHT");
		if (fht!=null) {
			ip = fht;
			inverse = true;
		} else
			inverse = false;
		ImageProcessor ip2 = ip.crop();
		if (!(ip2 instanceof FloatProcessor)) {
			ImagePlus imp2 = new ImagePlus("", ip2);
			new ImageConverter(imp2).convertToGray32();
			ip2 = imp2.getProcessor();
		}
		imp = fft(ip2, inverse);
                return imp; 
	}
	
	
	public ImagePlus fft(ImageProcessor ip, boolean inverse) {
		//IJ.write("fft: "+inverse);
		//new ImagePlus("Input", ip.crop()).show();
		int maxN = ip.getWidth();
		makeSinCosTables(maxN);
		float[] fht = (float[])ip.getPixels();
	 	rc2DFHT(fht, inverse, maxN);
		if (inverse) {
			ip.resetMinAndMax();
			new ImagePlus(imp.getTitle(), ip).show();///do something here to grab the image
		} else {
			ImageProcessor ps = calculatePowerSpectrum(fht, maxN);
			ImagePlus imp = new ImagePlus("FFT", ps);
			imp.setProperty("FHT", ip);
			imp.show();//same here, grab from me here, 
			if (IJ.altKeyDown()) {
				ImageProcessor amp = calculateAmplitude(fht, maxN);
				new ImagePlus("Amplitude", amp).show();
			}
		}
                return imp;
	}

@brian_mitchell If you want to get help quickly, please provide a minimal example (MCVE) and don’t expect people to sift through hundreds of lines of code.

1 Like

I edited it down to the two methods I changed from FFT_ class

Also, it would help if you post your code in GitHub so it can be easily run by others.