Swapping xy axis

Hi fellow Imagej-ers. I’m not new to IJ, but my prior experience was in processing sem images of my lava thin sections to be used with CSDCorrections (a program for analysing crystal size distributions in volcanic rocks). As a result my range of experience is limited to just a few functions used repeatedly. I have recently started a PhD and now need to map thin sections and based on a scribed reference point placed in the top right corner of the section in a portrait orientation (this is due to the orientation of the section mount). This is all good so far and do-able in IJ, As IJ defaults to the top left hand corner for its origin, However, the final demand which I cant seem to find an answer for is that the x axis has to run on the short edge from the origin at the top right and the Y axis along the long edge. After rotating the image through 270 degrees in a photo program, I can get the axis to the required positions and the Y along the long edge will operate correctly (i.e. the values read in ascending fashion away from the origin, however, the x axis wont play nicely at all and I can only get it to read in a descending order from the origin to the top left corner. In short is theer any way for me to reverse the X axis?

Hi, @AndyT. Welcome to the forum.

I tried to find older threads that talked about similar situation, I found just this one. Maybe what you are looking for is the operation

Image > Transform > Flip Horizontally / Flip Vertically

Not sure if I got it correctly but it is probably it. If it isn’t, maybe you could provide another explanation or an image.

I hope this helped. Cheers,

Hi Leandro. Thanks for you suggestion. I have had a look at this and it does not solve the basic problem for me, but maybe my explanation was not as clear as it could have been, so I’ll try again. When I load my slide image into IJ, it is a landscape image and the origin is by default in the top left corner. X axis is on the long axis and moving the cursor to the right (away from the origin along the x axis) produces an ascending pixel count as expected. The Y axis is on the short axis and moving the curser down (away from the origin on the short axis) also produces an ascending pixel count as expected. For my purposes (mapping points for analysis using LA-ICP-MS), I need to turn the image through 90 degrees clockwise and which puts the origin in the desired place (top Right). In this new configuration, the long right edge now registers as the y axis and moving the cursor down from the origin in the top RH corner, the pixel count for the Y axis behaves exactly as it should – ascending with distance along this edge away from the origin. However, the problem is the x axis which shows a descending pixel count from the origin (i.e. right to left). What I need to do, in the configuration I have described is to have an ascending pixel count from an origin in the top RH corner along te x axis. Is this possible? If not, How do I write a plug in to do it? Weird, I know. Any advice is greatly appreciated. Cheerio. Andy

Hi @AndyT,

as you state correctly, Flip Horizontally or Flip Vertically will alone not solve your task. You should however be able to achieve it by combining Image > Transform > Rotate 90 Degrees Right with Image > Transform > Flip Horizontally, so essentially these two lines of macro code:

run("Rotate 90 Degrees Right");
run("Flip Horizontally");

Alternatively, you might use an affine transformation flipping the x and y axes, e.g. the one provided by TransformJ (available via the ImageScience update site).

image image

Yet another alternative (involving some scripting) is to use ImageJ Ops, as illustrated (for a 3D xyz or xyc image) by the following Groovy script (simply run it from the script editor):

#@ Img img
#@ OpService ops
#@ UIService ui

mins = new long[img.numDimensions()]
maxs = new long[img.numDimensions()]

permuted = ops.run("transform.permuteView", img, 0, 1)
result = ops.run("transform.intervalView", permuted, mins[1, 0, 2], maxs[1, 0, 2])

Or do you only want to invert the x coordinate that is shown in the status bar when you move the mouse over the pixels? I don’t think this is possible in the ImageJ UI. You can set the pixel width in the properties to -1, but this will only lead to the coordinates running from 0 to -xMax and not put the origin to the top right corner…


By the looks of things then, based on your last couple of sentences, I cant achieve what I’m after as things stand. I guess I’m going to have to look at alternatives like doing all this completely manually in Illustrator or such like - not my first choice!! Anyway, Jan and Leandro, thanks for giving it a go. I appreciate your advice. All the best andyT

At least not in the way you imagined (or described) it. But maybe, if you elaborate on your goal (why do you need to have the coordinate inverted? do you need a live reporting of the coordinate? do you just need measurements?), we might find a satisfying solution for you.

For example, if all you need is to report a top-right-based coordinate in the results table upon clicking on a location in the image, you could use the following tool macro:

macro "Inverted x Coordinate Tool - T0914!T6914X" {
	getCursorLoc(x, y, z, flags);
	w = getWidth();
	n = nResults;
	setResult("inverted X", n, w-x-1);
	setResult("Y", n, y);

Hi again Jan. Thanks for staying interested in this one. my lack of IJ experience is probably self evident. I’m looking at mapping points of interest on my thin sections (of Volcanic rock samples) for my PhD (when I get them). The process is outlined in a paper written by one of my supes, but he used what strikes me as a bit of a long and complicated process where the scanned image is processed as a layered file in Illustrator and then outputted to Excel where various calculations are carried out to fix exact positions of points of interest against a predetermined mark mare on the top right edge of the slide when it is in portrait orientation. it has to be in portrait because of the peculiarities of the microprobe, and for the same reason, the origin has to be in that top right corner with the y axis in the right long edge and the x axis across the top short, both reading ascending from the origin. Does that make things any clearer? Thanks Jan. AndyT

I’m afraid not much (for me) :slight_smile:

Do you need to:

plot the axes along with the image?
have an interactive readout of the axis coordinates in the user interface?
have a recorded measurement for certain points in your image?
do some calculations based on the image data and these inverted/swapped axes?

Also, did my macro suggestion help in any way (you can install it by pasting it into a Plugins > New > Text Window… and then clicking on Macros > Install Macros in the menu)?

Would you mind linking to that paper?

Hi Jan. I had a printed copy. The paper is Mapping Rectangular Rock Thin-sections for repeatable Electon Microprobe Analysis: a step be step guide. I could get a citation for it on Google scholar and if it is any help here’s the reference for it

Tarff, R.W. and Day, S.J., 2010. Mapping rectangular rock thin sections for repeatable electron microprobe analysis. Microscopy and Analysis-UK, (139), p.19 - 21.

I cant find my copy of the paper amongst the mass of stuff cluttering my computer But if it makes things easier I’ll try to put a little detail on what I have already said. The method requires a single reference mark, which is the point defined by intersection of the long side edge of the slide with an edge-perpendicular scratch made on the top right corner of the front face of the slide with a diamond scribe. This is used as the (0,0) origin of an (x,y) grid - with 0.5 mm line spacing - constructed on a high-resolution scanned image of the polished section, using a graphics programme like Illustrator. Locations of points of interest are picked out on the slide under the microscope and then transferred to the scanned image; they are then defined in that (x,y) grid and listed in an Excel spreadsheet. Then the slide is placed on the electron probe microscope motorized stage lined up with one of the side edges (this is critical as you have to make sure that the slide is aligned straight relative to the stage). Then the position of the reference mark on the stage is measured for that particular session, and the Excel sheet to add the session coordinates for the (0,0) reference mark to all the coordinates for the points of interest, thus giving you session coordinates for all those points of interest. These session coordinates can be inputted into the probe and the motorized stage will take you to each of them in turn. The method isn’t accurate enough to take you straight to the points that you want to analyse, but at least they’ll be in the field of view of the backscatter electron image and you can go select analysis points manually. My idea has been to replace the (I Think rather clunky Illustrator stage) with Image J which I think has the potential to do a better job all round, but the X axis thing is stumping me. If you can find the paper it explains it better than I can. I did have a go with the macro suggestion, but it did not seem to make a difference (unless I was doing it wrong). Cheerio! Andy

1 Like

@AndyT, @imagejan

I read some parts of the referred paper and, it seems (please correct me if I misunderstood, in the end I haven’t read the whole thing) that the point of having an origin in to top right corner of the image is just because the microprobe you will use will operate based on that reference point (just like an automated microscope stand I assume)

The best person to answer would be andy, but I am using what Jan asked to see if I got it and find a solution.

In this case, the answer could be “have an interactive readout of the axis coordinates in the user interface” and “have a recorded measurement for certain points in your image”.

In this case, I think the solution could be something like the following:

  1. Let’s assume that you have a point of interest in(x,y) coordinates (10,15) and your image is m by n pixels, let’s say 100 x 300 pixels. This coordinates I am defining are the coordinates of the image, which have origin on the top left corner.

  2. You would then create a macro or some ImageJ Ops Groovy script where you could click that certain point and record the readout. Running this script, you click on that point. The y coordinate doesn’t have a problem doesn’t need any change, but, to adjust the coordinate of your image you must invert the coordinate to the value x' = m - x. In this case, your new x’ coordinate is x' = 100 - 10 = 90. This value then is what you are looking for. Then, your script would need to make this operation and record the point of interest of coordinates (90, 15).

  3. You would do that with all points of interest (click in every one of then) and then finally the program could output a n by 2 matrix that has all (x,y) coordinates of your points of interest.

I hope we are going towards the same direction for a solution and I am helping (not making things worse). Sorry that I can’t write a script at the moment.

1 Like

If you’re going to read out the point coordinates that you place in the image using the multi-point tool, then output to excel (e.g. save the results as a .csv), i think using the current imagej coordinate system is fine. Just apply a mathematical operation on the coordinates before saving them, which would be the image width minus the x coordinate. Image of 1000-by-1000 pixels at position (650,100) would be (1000-650)=350 x, 100 y.

Am i missing something?

Is this “transferred” by hand?

As an aside, you said you rotated your image outside of imagej, was this manually? I’m sure you could find an automated method to align the slide within imagej, to fully automate your processing (except for picking points).



Hey Rob. Thanks for your post. If you look at Leandro’s last post I think you and he had more or less the same idea. If I had thought a little longer and harder about it I might have come to this solution myself, but such is life for the learner (which is what I am). The method described in the paper does call for me to literally identify a point of interest with the petrological microscope and mark it by hand on a gridded illustrator image - I think I described the process as clunky in an earlier post. I know I can do a better and far more accurate job in IJ and thanks to you guys I feel like I’m well on the way to doing it. I had already decided off the back of Leandro’s and Jan’s advice over the last day or so to start learning some macro coding - see if I can make life a little easier for myself all round, and besides which it adds to my skill set… In the meantime thanks for your post and if you have any other tips i’m all ears. Thanks mate.
all the best

@AndyT, good that a solution is being worked on. And don’t worry, we’re all learners .

You can merge suggested and comments from @7rebor and @imagejan to start.

This link shows you some information on Macros. My suggestion is: start using Record macro Command first, If you are not very familiar with It. What happens is that, every operation or plugin you run, every window you select gets recorded on the macro Window (Just like in Excel If you have ever done that) and you can take a look at how the commands are written in macro language. It helps me a Lot and still helps on some of what I do.

Also, try using the multipoint Tool to select all the points of interest in your images as Rob said.


@AndyT, sorry, realised after i posted that I had repeated @leandroscholz (reading this whole thread on my phone).

I thought you were marking points on one program and saving an image, going to a new computer and re-plotting the points by looking between the two images. If this was the case, you could use image registration and transformation to align both images and plot the points automatically in ImageJ. But I assume you just have a computer next to a petrological microscope (that doesn’t have a camera?).

ImageJ is very powerful and a great piece of software to know how to use, best of luck and I hope it goes well.


Hey Rob. No apologies required mate. I’m just happy that so many people have taken an active interest in the problem and how to solve it.


Hi Guys - Leandro, Jan and Rob. Thank you so much for all your tips and advice over the last couple of days. I think I have enough to be going on with now and I’m going to give some coding a go - It will be a good learning curve for me. I’ll shout out in despair if I run into trouble again, so keep an eye on the forum just in case. And I’ve kept a copy of this post so I dont forget to credit you guys when I’m writing all this up in a couple of years. Thanks again guys!



1 Like

Hi again. Further top my previous posts, I am currently tying myself in knots over how to code the simple (apparently) process we have been discussing in this thread. I keep thinking I have solved it bu my solutions are getting ever more complicated and I cant believe this can be so. I can see what needs to happen. I can visualise it on paper, I just cant seem to get the scripting to work. Can someone put me out of my misery and tell me how to subtract point x from the width when your x axis is numbering in reverse order (i.e. 100 to 0 from the origin rather than 0 to 100 from the origin like it should)


Andy T

OK so this is probably a bad sign, but I cant sleep and I’m up a 3 in the morning trying to work it out. The functions I have to carry out to my image include turning it from landscape to portrait which puts the origin in the top right (which is where I want it to be). The Y axis is on the right long edge and does what it is supposed to do, but the x across the top operates in a decreasing manner from the origin to the left which id not good behaviour!!!. The solution from the rest of this thread is to mark my points and then subtract the (incorrect result from the width to get a true x value as if the x axis was behaving correctly, but no amount of messing with the macro I have been trying to create will make it do this. My macro is as follows:

macro “true x Coord” {
getCursorLoc(x, y, z, flags);
w = getWidth();
x = “Point”();
n = nResults;
setResult(“x’”, n, w - x);
setResult(“Y”, n, y);
function returnsub (x, w) {
x’ = w - x;

What am I doing wrong!!! Help!!




It would be good if someone could take a look at what you wrote for learning purposes, but I haven’t seen your post while I was writing a solution (it is late where I am as of now and I’ll sleep, but I can look at it tomorrow probably). What my solution provides is a .csv file that provides only the x and y coordinates of your points of interest. (copy this code in the imageJ macro writer Plugins>New>Macro and run it once you have your image rotated and flipped horizontally) Here it is:

//open your image, rotate it and flip horizontally, then run this macro

ImageID = getImageID();   

waitForUser("Define points", "After selecting all the points of interest in the image, click OK");

	waitForUser("Define points","There are no points selected! Please select points of interest, then click OK");



dir=getDirectory("Choose a Directory");
saveAs("Results", dir+"/Corrected_coord.csv");
1 Like