Background subtraction, segmentation and ROI calculation

Hello everyone,

I am very new to ImageJ, I hope I can learn from you in this challenging request.
For research, I want to do the following:
I have an ultrasound image of a cross-section of the heart (image 3). Using another app, I have performed background subtraction, which is why you see a red-yellowish band on the heart (the higher the signal the more yellow-white it is, the lower the signal, the more redish-black it is).
First I want to do automatic (if that is possible) inner and outer contour detection (image 1)
Then I want to segment the heart in 16 parts (with the green circle halfway between inner and outer circle, image 4). The angle between each of the 8 radial lines should be the same.
Then I want to calculate the mean ROI of each of these segments based on this red-yellow heat map (image 2).

I know that I ask a lot, it would be fantastic if someone can help me out with this one.

Thank you

Hi
@soufiane
Initial step

Can this be suitable? Observe the two circles: they are not concentric! This may be appropriate?
I propose to guide you:
Step by step

  1. Initial step (For internal and external detection)
  • use the plugin RGB to CMYK
  • use image Y
  • Image->Adjust->Threshold
  • Edit-> Selection-> Fit Circle
  • to get the inner circle after the outer you will have to reverse the binary image
    You get this.
    I’m waiting for your macro for this first phase.
    Then we will go to step 2: add an inner circle.

@Mathew
Thank you, that is exactly what I’m looking for.
In some images it may be more like an ellipse.
I don’t know how to write a macro.
I came as far as doing the threshold, then I get the image in four different colours.
When I want to fit a circle/ellipse, I should make a selection, but can this be done automatically?

For an abstract submission, it would be fantastic if I can run a macro and provide the results of the ROI analyses before coming Monday (100+ images).

Thank you!

Hi
@soufiane

To write a macro you have to use: plugin-> macro-> record
All your transactions are recorded.

I give you a first part of the macro.
I hope that you will like!

first part

run("Duplicate...", "title=1");
run("Duplicate...", "title=2");
run("RGB to CMYK");
selectWindow("CMYK_2");
run("Stack to Images");
selectWindow("Y");

setAutoThreshold("Default dark");
//run("Threshold...");
run("Convert to Mask");
// Draw external and internal circles
run("Analyze Particles...", "size=1000-Infinity exclude add");
roiManager("Select", 0);
run("Fit Circle");
roiManager("Add");
roiManager("Select", 1);
roiManager("Rename", "External");
roiManager("Select", 0);
roiManager("Delete");
roiManager("Show All without labels");
roiManager("Show None");
run("Invert");
run("Analyze Particles...", "size=1000-Infinity exclude add");

roiManager("Select", 1);
run("Fit Circle");
roiManager("Add");
roiManager("Select", 2);
roiManager("Rename", "Internal");
roiManager("Select", 1);
roiManager("Delete");
selectWindow("2");
roiManager("Show All without labels");
roiManager("Set Color", "red");
roiManager("Set Line Width", 5);
roiManager("Measure");

Second step
One of the circles should be refocused.
// Refocus the inner circle on the outer
Use:
Edit->Selection->Specify

Thank you Mathew for sharing the macro.
Which circle should be refocused? The inner bold one or the circle in between?
The circle in between (or ellipse actually) should be halfway in between the inner and outer circle.
Is there a macro for that?

Thanks,

Soufiane

Hi,

In addition to my previous comment:
I encounter the following problem when running the macro for circle drawing

Image 3 is what I want, but I get image 2.

What can I do to resolve this?

Thanks

The criteria to be taken into account have changed.
Your image dimensions change? Each macro works according to the criteria defined in the initial state.
Maybe others will help you.
Did you like the given macro?
By applying the ROI on your new photo we get this:
// open next image
roiManager(“Show None”);
roiManager(“Show All”);

Thank you matthew
Yes the images differ, they all have this red partices in a circular/ellipse pattern, but they differ in location and size. I hope someone can help me with a macro.
Also to detect the ellips halfway between inner and outer ellipse/circle.

Thanks again

@soufiane
I give you everything I did.
An image processed.

The macro that will only work for the first image.


run("Duplicate...", "title=2");
run("RGB to CMYK");
selectWindow("CMYK_2");
run("Stack to Images");
selectWindow("Y");

setAutoThreshold("Default dark");
//run("Threshold...");
run("Convert to Mask");
// Draw external and internal circles
run("Analyze Particles...", "size=1000-Infinity exclude add");
roiManager("Select", 0);
run("Fit Circle");
roiManager("Add");
roiManager("Select", 1);
roiManager("Rename", "External");
roiManager("Select", 0);
roiManager("Delete");
roiManager("Show All without labels");
roiManager("Show None");
run("Invert");
run("Analyze Particles...", "size=1000-Infinity exclude add");

roiManager("Select", 1);
run("Fit Circle");
roiManager("Add");
roiManager("Select", 2);
roiManager("Rename", "Internal");
roiManager("Select", 1);
roiManager("Delete");
selectWindow("2");
roiManager("Show All without labels");
roiManager("Set Color", "red");
roiManager("Set Line Width", 5);
roiManager("Measure");

// Refocus the inner circle on the outer

roiManager("Select", 0);
run("Specify...", "width=440 height=440 x=553 y=526 oval centered");
H = getResult ("Height", 1);
W = getResult ("Width", 1);
roiManager("Select", 1);
run("Specify...", "width=W height=H x=553 y=526 oval centered");

roiManager("Add");
roiManager("Select", 2);
roiManager("Rename", "New internal circle");
roiManager("Select", 1);
roiManager("Delete");
run("Clear Results");
roiManager("Measure");


//Calculation of position  of the median circle
BX0 = getResult ("BX", 0);
BX1 = getResult ("BX", 1);

BXm=(BX0+BX1)/2;
print(BXm);

BY0 = getResult ("BY", 0);
BY1 = getResult ("BY", 1);

BYm=(BY0+BY1)/2;
print(BYm);
// Diameter calculation

WX0 = getResult ("Width", 0);
WX1 = getResult ("Width", 1);
WXm=((BX0+BX1)/2);
print(WXm);

HY0 = getResult ("Height", 0);
HY1 = getResult ("Height", 1);
HYm=((HY0+HY1)/2);
print(HYm);

// Draw the median circle
selectWindow("2");
makeOval (BXm,BYm,HYm, HYm);
roiManager("Add");
roiManager("Select", 2);
roiManager("Rename", "Median");

roiManager("Set Color", "green");
roiManager("Set Line Width", 1);

// Draw diameter
x0= getResult("BX",0)+(getResult("Width",0)/2);
y0= getResult("BY",0)   ;


x1= getResult("BX",0)+(getResult("Width",0)/2) ;
y1= getResult("BY",0)+(getResult("Height",0)) ;

makeLine(x0, y0, x1, y1);

roiManager("Add");
roiManager("Select", 3);
roiManager("Rename", "Diameter 1");

roiManager("Select", 3);
run("Rotate...", "  angle=45");
roiManager("Add");
roiManager("Select", 4);
roiManager("Rename", "Diameter 2");
run("Rotate...", "  angle=45");
roiManager("Add");
roiManager("Select", 5);
roiManager("Rename", "Diameter 3");

run("Rotate...", "  angle=45");
roiManager("Add");
roiManager("Select", 6);
roiManager("Rename", "Diameter 4");

roiManager("Show All");
roiManager("Set Color", "green");
roiManager("Set Line Width", 5);
run("Flatten");
close("\\Others"); 
roiManager("reset") ;
print("\\Clear");
run("Clear Results");
run("RGB to CMYK");
selectWindow("CMYK_2-1");
run("Stack to Images");
selectWindow("C");
close("\\Others"); 
setAutoThreshold("Default");
//run("Threshold...");
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=1000-5900 exclude add");ype or paste code here

Thank you Matthew, I very much appreciate your help.
Thanks for the last macro, that is very helpful.

Suppose I draw the circles or ellipses manually in ImageJ, can I than use your macro to get the segmentation?

I can not answer your question. I do not know.

Please, mark this post as a solution (using the icon […] below the post).

Hi Matthew, I get the following error when running the code.
What should I change?

Thank you

@soufiane

Open this image

Use this macro (which works well at home).

run("Duplicate...", "title=2");
run("RGB to CMYK");
selectWindow("CMYK_2");
run("Stack to Images");
selectWindow("Y");

setAutoThreshold("Default dark");
//run("Threshold...");
run("Convert to Mask");
// Draw external and internal circles
run("Analyze Particles...", "size=1000-Infinity exclude add");
roiManager("Select", 0);
run("Fit Circle");
roiManager("Add");
roiManager("Select", 1);
roiManager("Rename", "External");
roiManager("Select", 0);
roiManager("Delete");
roiManager("Show All without labels");
roiManager("Show None");
run("Invert");
run("Analyze Particles...", "size=1000-Infinity exclude add");

roiManager("Select", 1);
run("Fit Circle");
roiManager("Add");
roiManager("Select", 2);
roiManager("Rename", "Internal");
roiManager("Select", 1);
roiManager("Delete");
selectWindow("2");
roiManager("Show All without labels");
roiManager("Set Color", "red");
roiManager("Set Line Width", 5);
roiManager("Measure");

// Refocus the inner circle on the outer

roiManager("Select", 0);
run("Specify...", "width=440 height=440 x=553 y=526 oval centered");
H = getResult ("Height", 1);
W = getResult ("Width", 1);
roiManager("Select", 1);
run("Specify...", "width=W height=H x=553 y=526 oval centered");

roiManager("Add");
roiManager("Select", 2);
roiManager("Rename", "New internal circle");
roiManager("Select", 1);
roiManager("Delete");
run("Clear Results");
roiManager("Measure");


//Calculation of position  of the median circle
BX0 = getResult ("BX", 0);
BX1 = getResult ("BX", 1);

BXm=(BX0+BX1)/2;
print(BXm);

BY0 = getResult ("BY", 0);
BY1 = getResult ("BY", 1);

BYm=(BY0+BY1)/2;
print(BYm);
// Diameter calculation

WX0 = getResult ("Width", 0);
WX1 = getResult ("Width", 1);
WXm=((BX0+BX1)/2);
print(WXm);

HY0 = getResult ("Height", 0);
HY1 = getResult ("Height", 1);
HYm=((HY0+HY1)/2);
print(HYm);

// Draw the median circle
selectWindow("2");
makeOval (BXm,BYm,HYm, HYm);
roiManager("Add");
roiManager("Select", 2);
roiManager("Rename", "Median");

roiManager("Set Color", "green");
roiManager("Set Line Width", 1);

// Draw diameter
x0= getResult("BX",0)+(getResult("Width",0)/2);
y0= getResult("BY",0)   ;


x1= getResult("BX",0)+(getResult("Width",0)/2) ;
y1= getResult("BY",0)+(getResult("Height",0)) ;

makeLine(x0, y0, x1, y1);

roiManager("Add");
roiManager("Select", 3);
roiManager("Rename", "Diameter 1");

roiManager("Select", 3);
run("Rotate...", "  angle=45");
roiManager("Add");
roiManager("Select", 4);
roiManager("Rename", "Diameter 2");
run("Rotate...", "  angle=45");
roiManager("Add");
roiManager("Select", 5);
roiManager("Rename", "Diameter 3");

run("Rotate...", "  angle=45");
roiManager("Add");
roiManager("Select", 6);
roiManager("Rename", "Diameter 4");

roiManager("Show All");
roiManager("Set Color", "green");
roiManager("Set Line Width", 5);
run("Flatten");
close("\\Others"); 
roiManager("reset") ;
print("\\Clear");
run("Clear Results");
run("RGB to CMYK");
selectWindow("CMYK_2-1");
run("Stack to Images");
selectWindow("C");
close("\\Others"); 
setAutoThreshold("Default");
//run("Threshold...");
setOption("BlackBackground", true);
run("Convert to Mask");
run("Analyze Particles...", "size=1000-5900 exclude add");

Hmm still getting the error message…

@soufiane
Have you set Set-Measurement?

Thank you Mathew,

I have made a macro using parts of your macro to semi-automatic create the segmented ROIs.

//Ask for user to place multi point for outer circle
waitForUser(“Place multiple points for outer circle”);
getSelectionCoordinates(x,y)
run(“Fit Circle”);
roiManager(“Add”);
roiManager(“Select”, 0);
roiManager(“Rename”, “Outer circle”);

//Ask for user to place multi point for inner circle
waitForUser(“Place multiple points for inner circle”);
getSelectionCoordinates(x,y)
run(“Fit Circle”);
roiManager(“Add”);
roiManager(“Select”, 1);
roiManager(“Rename”, “Inner circle”);
roiManager(“Show All”);

//Ask for user to place multi point for median circle
waitForUser(“Place multiple points for median circle”);
getSelectionCoordinates(x,y)
run(“Fit Circle”);
roiManager(“Add”);
roiManager(“Select”, 2);
roiManager(“Rename”, “Median circle”);
roiManager(“Show All”);

// Ask for user to draw a line, to define extents
waitForUser(“Draw a line”);
roiManager(“Add”);
roiManager(“Select”, 3);
roiManager(“Rename”, “Diameter 1”);
roiManager(“Select”, 3);
run(“Rotate…”, " angle=45");
roiManager(“Add”);
roiManager(“Rename”, “Diameter 2”);
roiManager(“Select”, 3);
run(“Rotate…”, " angle=90");
roiManager(“Add”);
roiManager(“Rename”, “Diameter 3”);
roiManager(“Select”, 3);
run(“Rotate…”, " angle=135");
roiManager(“Add”);
roiManager(“Rename”, “Diameter 4”);
roiManager(“Show All”);

This is what I get:

But now I want to get red of the inner parts of the lines in the inner circle and the outer parts of the lines outside the outer circle. So I want to keep only the 16 segments and calculate their ROIs.

Thanks

1 Like