Posted by
Juanjo Vega on
Apr 08, 2011; 4:14pm
URL: http://imagej.273.s1.nabble.com/How-to-center-an-ellipse-tp3685070p3685072.html
It works!! Thanks!! :)
On Apr 8, 2011, at 5:48 PM, Rasband, Wayne (NIH/NIMH) [E] wrote:
> On Apr 8, 2011, at 9:33 AM, Juanjo Vega wrote:
>
>> Hello!
>>
>> I'm trying to center an ellipse ROI in an ImagePlus.
>>
>> I have the following method, which gets a ROI, fits an ellipse, sets it's center (x and y), and then tries to refresh it, so the ellipse will be moved to the center of the image:
>>
>> public void fitEllipse() {
>> Roi roi = ip.getRoi();
>>
>> if (roi != null && roi.isArea()) {
>> IJ.run(ip, "Fit Ellipse", "");
>> ImageStatistics is = ip.getStatistics();
>> EllipseFitter ef = new EllipseFitter();
>> ef.fit(ip.getProcessor(), is);
>>
>> // Builds an ellipse...
>> ef.xCenter = ip.getHeight() / 2;
>> ef.yCenter = ip.getHeight() / 2;
>>
>> // IJ.run("Select None");
>> ef.makeRoi(ip.getProcessor());
>> }
>> }
>>
>> The last part doesn't work and I'm sort of ideas. Does anybody can help me, please?
>
> Here is JavaScript code that gets the current ROI, fits an ellipse to it and then moves the resulting elliptical ROI to the center of the image.
>
> -wayne
>
> imp = IJ.getImage();
> roi = imp.getRoi();
> if (roi!=null && roi.isArea()) {
> IJ.run(imp, "Fit Ellipse", "");
> roi = imp.getRoi();
> r = roi.getBounds();
> xcenter = r.x+r.width/2;
> ycenter = r.y+r.height/2;
> dx = imp.getWidth()/2-xcenter;
> dy = imp.getHeight()/2-ycenter;
> roi.setLocation(r.x+dx, r.y+dy);
> imp.draw();
> }
------------------------------------------------------------
Juanjo Vega (
[hidden email])
Unidad de Biocomputación. Laboratorio B-13.
Centro Nacional de Biotecnología. CNB-CSIC.
C\ Darwin, 3. Campus de Cantoblanco.
Universidad Autónoma de Madrid.
28049, Madrid, Spain.
http://www.cnb.csic.eshttp://www.biocomp.cnb.csic.es+34 91 585 4510
"Las mejores almas son capaces de los mayores vicios como de las mayores
virtudes, y aquellos que caminan despacio por el camino recto pueden
llegar más lejos que los que corren pero se apartan de él." - Discurso
del Método, René Descartes.