Simple get coordinates plugin not working for 2000x2000 above images

Hi

I have created below plugin to get coordinates of an image. The plugin seems to work fine for the image resolutions below 2000x2000, but for images between 2000x2000 upto 3000x3000 the coordinates I get are half the resolution. For images above 3000 its 1/3, for 4000 above is 1/4 and so on. Could anyone please look into my plugin and explain me why is it doing so.
Many thanks.
Harkirat

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

public class get_Coords extends PlugInFrame implements MouseListener {
	ImagePlus tempimp;
	int imageWidth;
	int imageHeight;
	ImageCanvas canvas;
	
	public void run(String arg) {
		OpenImageDialog();
	}

	public boolean OpenImageDialog(){
		OpenDialog od = new OpenDialog("Select Image");
		if(od.getDirectory() == null){
			return false;
		}
		String fileDirectory = od.getDirectory() + od.getFileName();		
		ImagePlus imp = new ImagePlus(fileDirectory);
		tempimp = imp;
		imp.show();
		imageWidth = imp.getWidth();
		imageHeight = imp.getHeight();
		IJ.log("Image Plus width : "  + imageWidth + " and height : " + imageHeight);
		ImageWindow win = imp.getWindow();
		canvas = win.getCanvas();
		canvas.addMouseListener(this);
		return true;
	}
	
	public void mousePressed(MouseEvent e) {		
		int x = e.getX();
		int y = e.getY();
		IJ.log("x : " + x + " y : " + y);
	}

	public void mouseReleased(MouseEvent e) { }
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseClicked(MouseEvent e) {}	
	
	public get_Coords () {
		super("Plugin_Frame");
	}
}

You are getting the screen coordinates. The values are changing if you are zooming in or out according to the visible canvas coordinates. You have to apply the the transformation to the image coordinates, e.g. simply with:

    public void mousePressed(MouseEvent e) {		
    		int x = canvas.offScreenX(e.getX());
                int y = canvas.offScreenX(e.getY());
    		IJ.log("x : " + x + " y : " + y);
    }

See:


(also for floating point coordinates if required)

2 Likes

Thanks very much Bio7, it works perfectly.

Cheers
Harkirat