Re: How to center an ellipse?
Posted by
Rasband, Wayne (NIH/NIMH) [E] on
Apr 08, 2011; 3:48pm
URL: http://imagej.273.s1.nabble.com/How-to-center-an-ellipse-tp3685070p3685071.html
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();
}