1

I used imellipse to select an ellipse as my region of interest (ROI). The issue is that the ellipse I want to select is of around 45 degrees, and, when I use imellipse, it seems it is 90 degrees either horizontally or vertically.

How can I change the orientation of the ellipse?

Thanks.

1 Answers1

1

You need to rotate the coordinates of an ellipse. Like this:

npts = 1e4;
t = linspace(0,2*pi,npts);
theta = pi/4;
aspect = [5 1]; % [x y]
x = aspect(1)*sin(t+theta);
y = aspect(2)*cos(t);
plot(x, y);

enter image description here


If you want to use imellipse to draw the ellipse on an image, you can extract the vertices and transform them:

figure, imshow('pout.tif');
h = imellipse;
exy = h.getVertices
theta = pi/12;
M = [cos(theta), sin(theta); -sin(theta), cos(theta)]
exy_centered = bsxfun(@minus,exy,mean(exy))
exyRot = bsxfun(@plus,exy_centered*M,mean(exy));
hold on
plot(exyRot(:,1),exyRot(:,2),'r') % orig: plot(exy(:,1),exy(:,2),'r')

enter image description here

To fill in the ellipse, creating a mask, use roifill or roipoly:

w=getfield(imfinfo('pout.tif'),'Width');
h=getfield(imfinfo('pout.tif'),'Height');
bw = roipoly(zeros(h,w),exyRot(:,1),exyRot(:,2));
chappjc
  • 30,359
  • 6
  • 75
  • 132
  • Thanks for your reply. Can I use that for selecting an ROI, such that the ROI be `white`, and the rest of the image as `balck`? –  Apr 04 '14 at 23:38
  • @user3481560 Sure, do you have the Image Processing Toolbox? – chappjc Apr 04 '14 at 23:44
  • Yes, I do have the image processing toolbox. Part of the program I'm working with, I did: `roi = imellipse;`, how can I combine your suggestion? Thanks a lot –  Apr 04 '14 at 23:52
  • @user3481560 Oh, I see now. You can extract the ellipse vertices and rotate the coordinates. I'll update my answer with an example... – chappjc Apr 04 '14 at 23:58
  • Thanks a lot for your replies. Sorry I couldn't vote up since I need to have `15` of reputation –  Apr 05 '14 at 01:22
  • @user3481560 No worries, when your rep comes up, just come on back. ;) – chappjc Apr 05 '14 at 01:42
  • @user3481560 Even though you can't vote up, you can accept the answer (make the green check mark next to the answer). Thanks! – chappjc Apr 07 '14 at 20:27