Scanning an analyze column by column

Hi everyone. I am trying to write my first macro. My aim is to take a rectangular Roi by the width of 1 pixel and height of optional. Then I need to divide the whole column under this 1pixel size and move to next pixel column in my image and repeat the same procedure (get the mean of first Roi and decide the whole column under it by the mean value.) If some one could give me a hint or any instructions that how I can define a loop to move over my image column automatically I would appreciate it.

The first component is to get the ROI and the operation we want to perform in the loop. In Fiji we can do this very easily by drawing the ROI and recording it with the Macro recorder: Plugins > Macros > Record…

This will give you this for measuring the mean value for instance in a rectangle of 1px width:

run("Set Measurements...", "mean redirect=None decimal=3");
makeRectangle(0, 0, 1, 100);
run("Measure");

The parameters of the rectangle function corresponds to the location of the upper left corner of the rectangle (x,y) and the width and height of the rectange (w,h). To loop this over an image thus you need to manipulate this function and loop over the x location:
makeRectangle(x, y, w, h);

In the above example we thus draw a rectangle at the location 0,0 with a width of 1px and a height of 100px.

Next component is the for loop. For this you consult the macro language documentation: https://imagej.nih.gov/ij/developer/macro/macros.html#loops
This defines the for loop like this:

for (initialization; condition; increment) {
      statement(s)
   }

Lets say we do this for an image with 600x412 px. You need to start with location 0 and increment by 1 px until 599. You would write the loop like this

for (i = 0; i < 600; i++) {
}

We define a variable i (variables are placeholder that store values like numbers and strings, we can recall the value by just calling the variable) that starts at 0 (starting location). We run the loop as long as this variable is smaller than 600, thus 599 is the last value we use. i++ denotes we increment the variable positively by 1. Then we can combine this with our macro:

run("Set Measurements...", "mean redirect=None decimal=3");

for (i = 0; i < 600; i++) {

	makeRectangle(i, 0, 1, 100);
	run("Measure");
	
}

Notice how one plugs in the variable i that is incremented into the function makeRectangle at the x location. So the rectange moves by 1px.

You can test the macro using the AuPbSn40.jpg test image:
File > Open Samples > AuPbSn 40

Like so:

run("AuPbSn 40");
run("Set Measurements...", "mean redirect=None decimal=3");

for (i = 0; i < 600; i++) {

	makeRectangle(i, 0, 1, 100);
	run("Measure");
	
}

As a next step in automation you can define certain values automatically. Lets say you do not want to specify the width of the image manually all the time. You can extract that information from the image using macro functions: https://imagej.nih.gov/ij/download/tools/source/macros/functions.html

2 Likes

Dear Christopher
Thanks a lot for mentioning the whole details. really helpful.