Outlining certain peri-lesion region

Hi Kaat,

Here is how you can do it:

Lesion area:

Enlarged:

Peri-lesion area step 1:

This is what you should get if you follow the instructions I gave you. Now you have to clear the part of the ROI that is outside the tissue. To do that, just draw a ROI that overlaps with the part you want to remove while pressing the Alt key (that subtracts that part from the ROI). And then you get:

Of course, the first thing you should do is to calibrate the image.

I used jpeg files because I don’t think it’s necessary for this to upload such big files. I hope it is not a problem.

Hope this helps

Here is what I’ve got by automatic tracing:
3-1-a-1_traces

The trace in cyan was generated by a commercial ImageJ-plugin without any pre-processing of the image. One has to define the starting x-position only and some parameters that define the smoothing and thresholding.

The yellow line was produce by shifting the cyan contour by 55.32 pixels that correspond to 200µm.

Regards

Herbie

Dear,

@jmazario: my outlined area does not enlarge. If I do it like you said, I have to fill in via edit>selection>enlarge 0,2mm (200µm), but it doesn’t enlarge with that number.

Regards,
Kaat

Kaat,

here is the result of a better approach which doesn’t shift the traced contour but enlarges it.
3-1-a_tracesX

The smoothing observed with the yellow line may be tolerable.

Regards

Herbie

Hi Kaat,

For this to work you need the outline of the lesion region to be a closed profile, not just an open line. Look at the first picture I sent. Could that be the problem?

Kaat,

if you have an excerpt of the lesion area similar to the one I used before (outside to the top of the image), and if you have a hand-drawn “Freehand Line”-selection of the lesion contour, then you can use the following ImageJ-macro to get the peri-lesion contour:

setBatchMode(true);
drawOverlay( "cyan" );
getSelectionCoordinates(xpoints, ypoints);
xpoints = Array.concat(xpoints,xpoints[xpoints.length-1],xpoints[0]);
ypoints = Array.concat(ypoints,0,0);
makeSelection("polygon", xpoints, ypoints);
run("Enlarge...", "enlarge=55"); // 200µm = 55.32pel
drawOverlay( "yellow" );
run("Select None");
setBatchMode(false);
exit();
function drawOverlay( color ) {
	roiManager("Add");
	roiManager("Set Color", color);
	roiManager("Set Line Width", 2);
	run("Add Selection...");
}

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.

HTH

Herbie

Hi Herbie,

That’s a nice piece of code but it doesn’t work that well in the whole image. I’ve run it several times in the test image and this is the result:

It would be great if you could get it to work properly. It would be a great macro to add to our library. I’ll give it a shot too, but I think I’ve failed when I tried before. I can’t find a way to automatically remove the part of the ROI that falls outside the tissue.

Thank you,

Javi

Good day Javi,

please read carefully!

[…] if you have an excerpt of the lesion area similar to the one I used before (outside to the top of the image) […]

This requirement is essential!

That’s a nice piece of code but it doesn’t work that well in the whole image.

Of course!

Perhaps you try it as I’ve described …

Regards

Herbie

PS:
As I’ve written as well, the original lesion contour must not be a closed selection (polygon) but a Freehand line.

PPS:
For those who like to test my macro, here

3-1-a_excerptLesionContour.tif.zip (132.7 KB)

is the excerpt from the sample image with the lesion contour as polyline selection.
(Just un-zip this file and open the image in ImageJ. Make sure the selection is visible in the image. Then paste my macro code to an empty macro window “Plugins >> New >> Macro” and run it.)

Hi there,

Sorry, Herbie. I was so eager to test the macro that I didn’t realize that you had already said that it would only work under that very specific set of conditions. My bad.

As I said before, I’ve previously tried (and failed) to write a macro that would enlarge the edge of a lesion to incorporate a peri-lesion area. I’ve given it a go again and came out with a code that will, if not fix the problem completely, at least reduce the amount of interaction that it requires to just a couple of steps. It should work in images acquired under similar conditions than the sample image and with the same magnification (BEWARE: the macro will calibrate the images to 0.2766 px/µm).

For the macro to work properly, it’s necessary to follow some rules:

  1. The image you want to work with should be open before you run the macro.

  2. When it prompts you to draw the lesion area, draw it as precisely as you can in the lesion/peri-lesion interface but close it back by going through an area free of tissue (see the first image I sent in a previous post). Use the Polygon selection tool or the Freehand selection tool. Do not use Segmented line or Freehand line because it will not work.

The result should be a ROI encircling the peri-lesion area. This ROI could be containing some parts outside the tissue. It should be easy enough to prune them out by drawing an overlapping ROI on the parts you want to remove while pressing the “Alt” key (that, will subtract the ROI you are drawing from the peri-lesion ROI).

Once you are happy with the result you can add it to the ROI manager (Ctrl+t) and/or draw it into the image (Edit>Draw).

I haven’t been able to make it work better than this. Any insights on how to improve it would be very welcome.

I am aware there are some code lines that are unnecessary, but they will only increase the running time of the macro by a few milliseconds.

Well, and after all this writing, here is the code:

title = getTitle;
run("Set Scale...", "distance=55.32 known=200 unit=µm"); //Calibrates the image in µm
setTool("polygon");	// Sets the "Polygon selections" tool. "Freehand selecions" should work too. Not "Freehand line"
waitForUser ("Draw the lesion area and THEN press OK. \nMake sure it is a closed region");
roiManager("Add");
roiManager("Select", 0);
roiManager("Rename", "Lesion");
run("Enlarge...", "enlarge=200");
roiManager("Add");
roiManager("Select", 1);
roiManager("Rename", "Enlarged");
roiManager("Select", newArray(0,1));
roiManager("XOR");
roiManager("Delete");
roiManager("Add");
roiManager("Select", 0);
roiManager("Rename", "Peri-lesion big");
roiManager("Add");
roiManager("Delete");
selectWindow(title); 
run("Duplicate...", " ");
run("8-bit");
setAutoThreshold("Huang");
run("Analyze Particles...", "size=1000000-Infinity include add in_situ"); //Creates a ROI with all the tissue area
roiManager("Select", 1);
run("Make Inverse");
roiManager("Add");
roiManager("Delete");
roiManager("Select", 1);
roiManager("Rename", "Outside");
roiManager("Select", newArray(0,1));
roiManager("XOR");
roiManager("Add");
roiManager("Select", 2);
roiManager("Select", newArray(0,2));
roiManager("AND");
roiManager("Add");
roiManager("Select", 3);
roiManager("Rename", "Peri-lesion");
roiManager("Select", 2);
roiManager("Select", 0);
roiManager("Delete");
close();
selectWindow(title);
roiManager("Select", 0);
roiManager("Delete");
roiManager("Select", 0);
roiManager("Delete");
roiManager("Select", 0);
//setForegroundColor(255, 255, 0);  //These three last lines are commented out because the final ROI may need some "prunning" before drawing it
//run("Line Width...", "line=2");
//roiManager("Draw");

Just copy it and paste it into an empty macro window (Plugins>New>Macro) and run it after opening the image to analyze.

I hope this is useful.

Cheers,

Javi

[…] under that very specific set of conditions.

Nothing specific, from my point of view, you only need to turn the original sample image by 90 degrees and that’s it.
Did you have a look at the example image excerpt that I’ve posted as zip-file?
Try my macro with this image!

If you prefer the whole image, here is the result as JPG:
3-1-a_origRot-1

Please note that the lesion contour (cyan) is not hand-drawn.

Regards

Herbie

Sorry Herbie, I guess I missunderstood you when you said it was essential to try it as you described it. Again, my bad. I think your code is an elegant way of addressing the problem at hand. I never meant to disrespect you in any way. I apologize if I gave you that impression.

1 Like

Dear,

I’ve used the zip-file you send (@anon96376101) and tried the macro. It worked, but when I opened this zip-file, there already was some kind of contour, how can I add to other tiff-files? This is something I don’t really understand. Is it via that commercial ImageJ plugin you talked about? Can you maybe explain how this works? If this would work, I would be so happy.

Kind regards,
Kaat

Good day Kaat,

you wrote:

What I have drawn now is done by hand. I don’t see another option.

I think you mean the lesion contour and you are right, the selection in my zipped image file is meant to be the lesion contour but it wasn’t hand drawn. As you’ve conjectured, it was drawn semi-automatically by the commercial ImageJ-plugin “Horizontal Tracing”.

For my macro, it doesn’t matter, if you draw the contour by hand, using e.g. the “Freehand Line”- or the “Segmented Line”-tool from the ImageJ-toolbar.

As soon as you’ve finished drawing the lesion contour, you can start my macro and it will automatically draw the peri-lesion contour with a distance of 200µm from the lesion contour.

HTH

Regards

Dear,

Ok thank you very much for your help. I can’t download the plugin “horizontal tracing”, but it does work if I draw the contour by myself.

Kind regards,
Kaat

Kaat,

you can’t download it because you need to buy a license first!

If you are interested in a license, please contact me per message off-Forum.

Regards

Herbie

always hard to get that some code requires license and not everything is free :slight_smile:

Come on Emanuele,

how do you make your living?

Already forgotten: Past Saturday was the 200th birthday of Marx.
Work has to be adequately remunerated – no?

Any complaints about having provided gratis help on this Forum in many hundreds of posts?

Best

Herbie

1 Like

yes yes I fully agree with you,
but I noticed that a lot of people don’t get easily that sometimes codes are not free.

Hi,

Is it possible to measure the mean gray value between these 2 lines (200µm outlined area)? So can I take this as a ROI and measure the value only this selected area? Now I tried it with Image>adjust>Threshold and than Analyze>measure and my results are for the whole picture and not only for the selected area.

kind regards,
Kaat

Hi Kaat,

Have you tried the method I suggested? If you do, you should get a ROI that defines your perilesion area and then you can measure just inside that ROI. You could use the fully manual method I described or the semi-automated method with the macro code I sent. It is laborious but it works. We have used it succesfuly before to do exactly what you need to do (but in fluorescence images).

HTH