Combining / subracting ROIs

roi-manager
roi
Tags: #<Tag:0x00007fb8905709b8> #<Tag:0x00007fb890570468>

#1

If you run the macro below, there is a “polygon roi” and an “original” line ROI created. After the “OK” two additional red lines are added - I am looking for a way to get these two lines (i.e. where the original line is overlaying the polygon) automatically in a macro. Is this possible?

newImage("Untitled", "8-bit black", 500, 500, 1);
roiManager("reset");
roiManager("Show All without labels");

makePolygon(316,126,148,168,148,286,321,327,322,256,206,229);
roiManager("Add");
roiManager("select",0);
roiManager("Rename", "polygon");
roiManager("Set Color", "green");

makeLine(260, 103, 260, 343);
roiManager("Add");
roiManager("select",1);
roiManager("Rename", "original line");
roiManager("Set Color", "green");
roiManager("Set Line Width", 3);//Line Width only changed to see the original line below the new ones
								//generated in the next step

waitForUser;

makeLine(260, 139, 260, 178);
roiManager("Add");
roiManager("select",2);
roiManager("Rename", "new line1");
roiManager("Set Color", "red");

makeLine(260, 241, 260, 312);
roiManager("Add");
roiManager("select",3);
roiManager("Rename", "new line2");
roiManager("Set Color", "red");

run("Select None"); 

#2

Hi Martin,

this converts the line to an area, so perhaps not quite what you’re after but it might suffice.

Cheers,

Chris

newImage("Untitled", "8-bit black", 500, 500, 1);
roiManager("reset");
roiManager("Show All without labels");

makePolygon(316,126,148,168,148,286,321,327,322,256,206,229);
roiManager("Add");
roiManager("select",0);
roiManager("Rename", "polygon");
roiManager("Set Color", "green");
 
makeLine(260, 103, 260, 343);
run("Line to Area");   // This step converts the line to area
roiManager("Add");
roiManager("select",1);
roiManager("Rename", "original line");
roiManager("Set Color", "green");
roiManager("Set Line Width", 3);//Line Width only changed to see the original line below the new ones
								//generated in the next step

// create a new selection by taking the AND of the first two ROIs
roiManager("select",newArray(0,1));
roiManager("and");

// Add to the manager
roiManager("Add");
roiManager("select",2);
roiManager("Rename", "new line2");
roiManager("Set Color", "red");

run("Select None");

#3

Thanks @evenhuis. After splitting the new composite ROI I can convert back to line and have exactely what I asked for

newImage("Untitled", "8-bit black", 500, 500, 1);
roiManager("reset");
roiManager("Show All without labels");

makePolygon(316,126,148,168,148,286,321,327,322,256,206,229);
roiManager("Add");
roiManager("select",0);
roiManager("Rename", "polygon");
roiManager("Set Color", "green");

makeLine(260, 103, 260, 343);
run("Line to Area");   // This step converts the line to area
roiManager("Add");
roiManager("select",1);
roiManager("Rename", "original line");
roiManager("Set Color", "green");
roiManager("Set Line Width", 3);//Line Width only changed to see the original line below the new ones
							//generated in the next step

roiManager("select",newArray(0,1));
roiManager("and");
roiManager("Add");
roiManager("select",2);
roiManager("Rename", "composite area");

roiManager("Split");
roiManager("select",2);
roiManager("delete");
roiManager("Select", newArray(2, 3));
run("Area to Line");
roiManager("Set Line Width", 1);
roiManager("Set Color", "red");

run("Select None"); 

#4

Hey @MartinH,

that’s a neat trick with split, I’d tried running Area to Line on the composite.

Thanks,

Chris


#5

Except that you get a closed polyline, i.e. the outline of your desired line, no?

image


#6

You are right. Too sad, because I need to get the profile from this “line”.


#7

This is getting way more complicated than I expected, but I think I found a workaround I can live with:

newImage("Untitled", "8-bit black", 500, 500, 1);
roiManager("reset");
roiManager("Show All without labels");

makePolygon(316,126,148,168,148,286,321,327,322,256,206,229);
roiManager("Add");
roiManager("select",0);
roiManager("Rename", "polygon");
roiManager("Set Color", "green");

makeLine(260, 103, 260, 343);
print("linei: "+selectionType());
run("Line to Area");   // This step converts the line to area
roiManager("Add");
roiManager("select",1);
roiManager("Rename", "original line");
roiManager("Set Color", "green");

roiManager("select",newArray(0,1));
roiManager("and");
roiManager("Add");
roiManager("select",2);
roiManager("Rename", "composite area");

roiManager("Split");
roiManager("select",2);
roiManager("delete");

for (i=2; i<4; i++) {
	roiManager("Select", i);
	//run("Area to Line");// as pointed out by Jan its actually a polyline
	// the above line can now be omitted
getSelectionCoordinates(x, y);//retrieves array with the corner coordinates of the polyline
	Array.getStatistics(x, xmin, xmax, mean, stdDev);
	Array.getStatistics(y, ymin, ymax, mean, stdDev);
	makeLine(xmin, ymin, xmin, ymax);// my lines are always vertical, therefore xmin at both ends
	roiManager("Add");
}

roiManager("Select", newArray(2, 3));
roiManager("Delete");
run("Select None");