Repeat segmented line selection

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

That’s odd. It works for me as @Bio7 posted it. Please check again to make sure you have copied correctly. No extra spaces in the commands or variable names, no missing parentheses or semicolons, no extra characters introduced by copying and pasting, etc.

I put the pixel size as 0.45 microns based on 2.18 pixels per micron, not microns per pixel.

However, now that I look at it, this also does not match the scale bar on the image. That suggests 0.79 pixels/micron or 1.266 microns/pixel.

@mbkenawy, you should verify the scale with a stage micrometer. Otherwise your areas will not be measured correctly.

1 Like

I have evaluated the macro line by line, and there are lines which it doesn’t like.
pixelDist = dist*pixelSize;
Error msg: pixelDist= <dist>* pixelSize;

for ( i=0; i < num; i++) {
Error msg: for ( i=0; i < <num>; i ++)}

roiManager("Select", i);
Error msg: roiManager ("Select" , <i>);

roiManager("translate", 0, -pixelDist);
Error msg:')' expected in line 1
roiManager ("translate" <,> 0, -pixelDist);

roiManager("Add")};
Error msg: Statement cannot begin with '}' in line 1
roiManager ("Add") <}>

I am surprised that it worked with you.
May be it is related to my version1.48.
As regards the scale, I will recheck it after creation of the lines

regards

I’m sorry, but the error messages as written don’t make sense. It will be a lot easier to fix this issue if you can copy and paste your information instead of transcribing it, as this process introduces new and irrelevant errors.

  1. Copy your entire macro code out of the macro editor window in ImageJ. Then paste it into your post in the forum.
  2. Format your pasted code as code (looks like typewriter font): Select it with the mouse, then click the </> tool in the forum’s post editor.
  3. If you get errors, take a screenshot and paste the image into the forum using the upload tool (just to the right of the </> tool).

Just checking – you are pasting the code directly into the macro editor within ImageJ, right? Don’t use Word or some other word processor, because it may introduce invisible characters that can cause this sort of syntax error.

If the scale is wrong, your lines will be wrong.

1 Like
roiManager("Add");
pixelSize=2.18
dist=10 
pixelDist = dist*pixelSize;
num = 25;
for ( i=0; i < num; i++) {
roiManager ("Select", i);
roiManager ("translate", 0, -pixelDist);
roiManager ("Add")};
roiManager ("Show All");
1 Like

OK, that code works for me on Fiji and “vanilla” (non-Fiji) ImageJ.

My Fiji version:

My ImageJ:

1 Like

Thx tswayne and Bio7 for your patience and help.

It seems that the problem with my ImageJ version (V 1.48). I have downloaded a newer version and it seems to work.
However I can’t find my original segmented line, even if I add it on roiManager.
Also in the older version there was a cell counter tool, where you can count different types of cells. I can’t find this tool in the newer version!

Many thanks and sorry for overloading,

Regards,

@tswayne Totally off topic, but FYI, since you use OS X: you can quickly take a screenshot of a single application window by pressing Shift+Command+4, then pressing the space bar, then clicking the window of interest. If you do it that way, you’ll get a nice PNG with transparency including drop shadow. :grin:

2 Likes

Hi tswayne and Bio7,

I still have the issue where I can’t find the original segmented line that I have created even if I add it on the roi manager.
And I can’t find the cell counter tool in the newer version of ImageJ.

Regards,

Much classier! Thanks for the tip, @ctrueden!

The Cell Counter is in Fiji. You can search for any command by typing “L” (for locate or launch). If you have regular ImageJ you may have to find and download the plugin separately. But I recommend the Fiji package.

I see that there is a problem with the macro, that the first line is not preserved.
But if you add it twice and iterate through starting with the second ROI, then it gives you your first line and all the others. The last line also appears twice in the ROI manager – I’m not sure why, but I think it’s acceptable.

Below is a corrected version.

roiManager("Add");
roiManager("Add");  //add an extra copy to preserve original line
pixelSize=2.18
dist=10 
pixelDist = dist*pixelSize;
num = 5;
for ( i=1; i < num; i++) {
roiManager ("Select", i);
roiManager ("translate", 0, -pixelDist);
roiManager ("Add")};
roiManager ("Show All");
1 Like