Repeat segmented line selection

I have drawn a segmented line and I want to repeat every it every 10 microns, but I don’t know how to do this

Do you want to interpolate your segemented line to a specific distance? Then you can use the following command (example macro) which interpolates a segmented line selection:

run("Blobs (25K)");
makeLine(14,116,59,116,111,116,177,115,177,164,114,164);
run("Interpolate", "interval=10 adjust");
run("Properties... ");

Or do you want to copy your segmented line with a specified distance?
You can use the Roi manager,move the selection and then duplicate it (add it to the ROI manager).

Here an example adapted from:
http://imagej.net/macros/RoiManagerMoveSelections.txt

dx = 20;
dy = 20;
Dialog.create("Move Selections");
Dialog.addNumber("X Displacement:", dx);
Dialog.addNumber("Y Displacement:", dy);
Dialog.show();
dx = Dialog.getNumber();
dy = Dialog.getNumber();;
n = roiManager("count");
   if (n==0)
     exit("The ROI Manager is empty");
    for (i=0; i<n; i++) {
          roiManager('select', i);
          getSelectionBounds(x, y, w, h);
          setSelectionLocation(x+dx, y+dy);
          roiManager('Add');
     }

See also the function getScaled, etc. to get or set scaled coordinates.:

http://imagej.net/developer/macro/functions.html#toScaled

2 Likes

Thanks for your fast reply,

I want to copy the segmented line with a specified distance of 10 microns for 25-30 times, so the overall moved distance from the original position will be between 250-300 microns.
I have an issue in moving the selection in microns and not in Pixels, even though I have calibrated the slide. Also I don’t know how to duplicate the selection, each time I select duplicate it duplicates the whole slide without the selection.
And how can I view all the 25-30 lines at the same time?

Regards,

Here is a macro in float precision. I tried to create a macro with the functions

getSelectionBounds(x, y, w, h);
setSelectionLocation(x+dx, y+dy);

However I always got integer precision with the get bounds method, etc.

So finally I created a selection programmatically in this example.

You have to create a scaled image before (please adapt the scale to your needs).

Here we use a known 300 µm distance as a dummy example. The macro will create a repeated line in subpixel precision to show correct distances, e.g. if the image is very small:

distance=300//µm - image has to be scaled!
dist=10//µm - the distance interval.
num=distance/dist;//How many selections do we have to create.
toUnscaled(dist);//Get the scaled distance in pixel coordinates!
//print(dist)
x1=64.7;//Some dummy coordinates for a line
x2= 60.9;
makeLine(x1, 55.7, x2, 59.8);//Create a line or segmented line. Here we use a simple line!
roiManager('Add');//Add the line selection to the ROI Manager.
//Make a ROI pattern in µm distance and add the ROI's to the Manager!
for (i=0; i<num; i++) { 
    x1=x1+dist;
    x2=x2+dist;
    makeLine(x1, 55.7, x2, 59.8);//We don't change the vertical coordinates!
    roiManager('Add');//Add the line selection to the ROI Manager!     
    
 }
roiManager("Show All");//Show all selections of the ROI Manager.

Thanks sir,

But where can I download this macro?
I an sorry but I don’t understand the programming language, so if you can email me the macro ready for download I would be grateful.

Regards,

You can copy the text directly from @Bio7’s post and paste it into a text file or the macro editor window. See the user guide for more details.

Hope this helps.


I have copied the macro and installed it but it didn’t work. Here is a section with the segmented line at the bottom. I have scaled the image at 2.18 pixels/micron.
Now I need to repeat this line for 25-30 times every 10 microns and know the area between the selections.

Many thanks and sorry for overloading,

Hi,

Can you clarify what you mean by “the area between the selections”?
The pink area?

Hi,

I mean the area that will lie between two segmented lines that should be 10 microns apart,


It’s should be like a grid, but instead of horizontal lines it should be segmented lines parallel to the lower one.
And the area will be the area between two segmented lines that are 10 microns apart.

I understand you want to have segmented lines at 10-µm intervals. I mean the area you want to measure is presumably the pink-stained area, that is, excluding the white holes? Otherwise the area would be the same every time, right?

That is significant because it determines the methods you need to solve the problem. We have been talking about line selections, but I think you will need a polygon selection to measure areas.

And if you want to exclude the white spaces, then you will want to add a thresholding step.

2 Likes

No need to exclude the white holes. This will make it more difficult.
I presume the area should be the same between the selections.

Thanks for the info. I’m still confused though.

So why repeat the measurement 20-30 times?

What is the information you are looking to get from this image, in scientific terms?

I will count the cells between the lines, and from the area (which should be the same), I will be able to calculate the density.
This is a corneal section. Part of my PhD deals with comparing the cell density in the posterior part of he cornea, which is lined by the first segmented line, and then comparing it with other areas.

2 Likes

Thanks @mbkenawy, it helps a lot to know a bit of the science behind it.

To do the counting manually, you can use this modified version of @Bio7’s code that uses the “translate” feature of the ROI Manager to shift the line upward. First draw your line, then run the macro.

A quick and dirty way to get the area in each section of the “grid” is to have the lines showing, then use the polygon ROI tool to trace all the way around one section and measure the area with Analyze > Measure. Make sure you have the scale set correctly first (Analyze > Set Scale).

In the long run, I’m sure you could write a script or macro to define the grid sections as areas and count the nuclei in each one, to get cell density relative to position within the cornea. ImageJ is certainly capable of that. But it would take more time to figure out how to do that.

Hope this helps.


// you need to place a segmented line selection on the image before running the macro

roiManager("Add"); // adds the selection you made

pixelSize=0.4587 // image pixel size in µm - based on 2.18 pixels per micron
dist=10 // the distance interval in µm
pixelDist = dist/pixelSize; // distance interval in pixels

num = 30;  // number of repetitions

for (i=0; i<num; i++) { 
	roiManager("Select", i);
	roiManager("translate", 0, -pixelDist);  // negative number makes the next line closer to the top of the image
	roiManager("Add");
}
roiManager("Show All"); //Show all selections of the ROI Manager.
1 Like

I am sorry, but I am a bit confused.
I have copied the last macro and it didn’t work.
I should be using the last macro or Bio7 macro?

The macro which is closest to my need is ROI move selection macro, which Bio7 developed, but it still can’t display all the selections at the same time.

I will do the counting manually using the cell counter function of ImageJ, but first I need to create the lines.
Which is the macro that uses the translate feature of the ROI manager?

Use the script of @tswayne. Before you excute the script draw your segmented line!
Copy and paste the script. Pay attention to avoid copy artifacts from the forum!

Here a slight modification with your values (which are not congruent with the example image according to the ruler):

//2.18 pixels/µm
// you need to place a segmented line selection on the image before running the macro

roiManager("Add"); // adds the selection you made

pixelSize=2.18 // image pixel size in µm - based on 2.18 pixels per micron
dist=10 // the distance interval in µm
pixelDist = dist*pixelSize; // distance interval in pixels

num = 30;  // number of repetitions

for (i=0; i<num; i++) { 
    roiManager("Select", i);
    roiManager("translate", 0, -pixelDist);  // negative number makes the next line closer to the top of the image
    roiManager("Add");
}
roiManager("Show All"); //Show all selections of the ROI Manager.

Apropos you can create an area selection from a line selection, too (select ROI->adjust line width in toolbar->Line to Area). But then you need to adjust the lines, too.

1 Like

Hi Bio7,

I have copied the macro, but a macro error msg comes up:
)’ expected in line 13
roi manager (“translate” <,> 0, -pixel distance