Posted by
Stein Rørvik on
URL: http://imagej.273.s1.nabble.com/Centering-a-ROI-on-a-light-spot-tp5023983p5023986.html
What I usually do is to centre the ROI based on the centre of mass. I just calculate the offset between the mass centre and the geometrical centroid, and shift the ROI by this amount.
The advantage with this method is that you get a "weighted" centering, not dependent on any max value that may lie in a random place. See this macro for an example:
newImage("Untitled", "8-bit black", 500, 500, 1);
makeOval(156, 169, 22, 22);
run("Fill", "slice"); //draw a white spot
makeOval(117, 127, 135, 135); //make an off-centre ROI
waitForUser("Please continue to auto-center ROI based on mass centre");
centroidX = getValue("X raw");
centroidY = getValue("Y raw");
centreOfMassX = getValue("XM raw");
centreOfMassY = getValue("YM raw");
print("Centroid: ", centroidX, centroidY);
print("Centre of Mass: ", centreOfMassX, centreOfMassY);
offsetX = centreOfMassX - centroidX;
offsetY = centreOfMassY - centroidY;
print("Offset: ", offsetX, offsetY);
Roi.getBounds(x, y, width, height)
Roi.move(x + offsetX, y + offsetY);'
The disadvantage with the above macro is that it requires the entire object of interest to be inside the ROI. That is, the edges of the ROI should be near zero. An example where this macro does NOT work well is the Galaxy example image:
run("M51 Galaxy (16-bits)");
makeOval(138, 234, 58, 60);
//repeat above macro from here
Here the macro is NOT able to centre the ROI on the centre of the galaxy as you wanted, as there are too many "background" stars which also contribute to the centre of mass. The macro works however if you repeat it a few times, iterating the ROI upon the mass centre.
Stein
-----Original Message-----
From: ImageJ Interest Group <
[hidden email]> On Behalf Of CARL Philippe (LBP)
Sent: 30. september 2020 15:36
To:
[hidden email]
Subject: Centering a ROI on a light spot
Dear all,
Let's say I have a circular ROI which is placed (but not precisely) on top on a light spot.
Is there an efficent / easy method to center this ROI on the spot?
I thank you very much on your lightings on this.
My best regards,
Philippe
Philippe CARL
Laboratoire de Bioimagerie et Pathologies UMR 7021 CNRS - Université de Strasbourg Faculté de Pharmacie
74 route du Rhin
67401 ILLKIRCH
Tel : +33(0)3 68 85 42 89
--
ImageJ mailing list:
https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&data=02%7C01%7Cstein.rorvik%40sintef.no%7Cdb9fdc40aa284c8aa16408d86546a57c%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C1%7C637370701403031839&sdata=jevzl8uSkRmqIjo5uKt55uebjoL1FbYd7wf0V6wDsGw%3D&reserved=0--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html