Discussion:
draw ellipse on image
(too old to reply)
gilad
2011-03-03 20:46:19 UTC
Permalink
hello,

I have a binary image (matrix) that i wish to draw an ellipse on.
The eccentricity, major & minor axes, and center are known.
How to do this?

Thanks,
Gilad.
Bruno Luong
2011-03-03 21:40:24 UTC
Permalink
Post by gilad
hello,
I have a binary image (matrix) that i wish to draw an ellipse on.
The eccentricity, major & minor axes, and center are known.
How to do this?
% Image resolutions
x = 1:100;
y = 1:100;
[X Y] = meshgrid(x,y);

% Ellipse data, axis1 and axis2 are ellipse semi-axis and must be orthogonal
axis1 = [-30.6582563199015; -10.0586237724638];
axis2 = [ -4.42290008808483; 13.48081085893978];
center = [50; 60];

P = [axis1 axis2];
H = P*P';

DX = X(:)-center(1);
DY = Y(:)-center(2);
D = [DX DY]*sqrtm(inv(H));
Z = sum(D.^2,2);
Z = reshape(Z, size(X));
c = contourc(Z,1+[0 0]);
xe = round(c(1,2:end));
ye = round(c(2,2:end));
Z = zeros(size(Z));
% you should manage overflowed coordinates here
Z(sub2ind(size(Z),ye,xe)) = 1;

% Check
figure
imagesc(Z)
hold on
plot(center(1),center(2),'wo');
plot(center(1)+[0 axis1(1)], center(2)+[0 axis1(2)], 'w')
plot(center(1)+[0 axis2(1)], center(2)+[0 axis2(2)], 'w')
axis equal

% Bruno
Seyed Hamid Reza Sanei
2014-07-21 18:47:10 UTC
Permalink
Post by Bruno Luong
Post by gilad
hello,
I have a binary image (matrix) that i wish to draw an ellipse on.
The eccentricity, major & minor axes, and center are known.
How to do this?
% Image resolutions
x = 1:100;
y = 1:100;
[X Y] = meshgrid(x,y);
% Ellipse data, axis1 and axis2 are ellipse semi-axis and must be orthogonal
axis1 = [-30.6582563199015; -10.0586237724638];
axis2 = [ -4.42290008808483; 13.48081085893978];
center = [50; 60];
P = [axis1 axis2];
H = P*P';
DX = X(:)-center(1);
DY = Y(:)-center(2);
D = [DX DY]*sqrtm(inv(H));
Z = sum(D.^2,2);
Z = reshape(Z, size(X));
c = contourc(Z,1+[0 0]);
xe = round(c(1,2:end));
ye = round(c(2,2:end));
Z = zeros(size(Z));
% you should manage overflowed coordinates here
Z(sub2ind(size(Z),ye,xe)) = 1;
% Check
figure
imagesc(Z)
hold on
plot(center(1),center(2),'wo');
plot(center(1)+[0 axis1(1)], center(2)+[0 axis1(2)], 'w')
plot(center(1)+[0 axis2(1)], center(2)+[0 axis2(2)], 'w')
axis equal
% Bruno
Hi.
I find the above code very helpful, I am trying to draw an ellipse in an image similar to the above case, but I want to have the ellipse with full white pixels. I think my case should be the easier case of this code, but I am not sure which modification I need to make to the code to make it work for my case.
Your help is highly appreciated.
Hamid
Bruno Luong
2014-07-22 05:35:15 UTC
Permalink
Post by Seyed Hamid Reza Sanei
Hi.
I find the above code very helpful, I am trying to draw an ellipse in an image similar to the above case, but I want to have the ellipse with full white pixels. I think my case should be the easier case of this code, but I am not sure which modification I need to make to the code to make it work for my case.
color of the intensity image is defined by the colormap. Add this command
Post by Seyed Hamid Reza Sanei
colormap(gray)
of the script you'll get white ellipse.

For RFB image you need to set all the three colors plane to its saturation value to get white color.

Bruno
Zhiyi Liu
2016-08-12 03:13:03 UTC
Permalink
Hello Bruno,

Your code is very very helpful! May I please ask a question that if I want to fill in the region of the ellipse (I want to create a binary mask of a ellipse), which part of the code I should modify? Thank you very much!

Best,

Zhiyi
Post by Bruno Luong
Post by gilad
hello,
I have a binary image (matrix) that i wish to draw an ellipse on.
The eccentricity, major & minor axes, and center are known.
How to do this?
% Image resolutions
x = 1:100;
y = 1:100;
[X Y] = meshgrid(x,y);
% Ellipse data, axis1 and axis2 are ellipse semi-axis and must be orthogonal
axis1 = [-30.6582563199015; -10.0586237724638];
axis2 = [ -4.42290008808483; 13.48081085893978];
center = [50; 60];
P = [axis1 axis2];
H = P*P';
DX = X(:)-center(1);
DY = Y(:)-center(2);
D = [DX DY]*sqrtm(inv(H));
Z = sum(D.^2,2);
Z = reshape(Z, size(X));
c = contourc(Z,1+[0 0]);
xe = round(c(1,2:end));
ye = round(c(2,2:end));
Z = zeros(size(Z));
% you should manage overflowed coordinates here
Z(sub2ind(size(Z),ye,xe)) = 1;
% Check
figure
imagesc(Z)
hold on
plot(center(1),center(2),'wo');
plot(center(1)+[0 axis1(1)], center(2)+[0 axis1(2)], 'w')
plot(center(1)+[0 axis2(1)], center(2)+[0 axis2(2)], 'w')
axis equal
% Bruno
Bruno Luong
2016-08-29 16:14:03 UTC
Permalink
Post by Zhiyi Liu
Hello Bruno,
Your code is very very helpful! May I please ask a question that if I want to fill in the region of the ellipse (I want to create a binary mask of a ellipse), which part of the code I should modify? Thank you very much!
Here is the adaptation to fill the ellipse:

% Image resolutions
x = 1:100;
y = 1:100;
[X Y] = meshgrid(x,y);

% Ellipse data, axis1 and axis2 are ellipse semi-axis and must be orthogonal
axis1 = [-30.6582563199015; -10.0586237724638];
axis2 = [ -4.42290008808483; 13.48081085893978];
center = [50; 60];

P = [axis1 axis2];
H = P*P';

DX = X(:)-center(1);
DY = Y(:)-center(2);
D = [DX DY]*sqrtm(inv(H));
Z = sum(D.^2,2);
Z = reshape(Z, size(X));
Z = Z <= 1;

% Check
figure
imagesc(Z)
hold on
plot(center(1),center(2),'wo');
plot(center(1)+[0 axis1(1)], center(2)+[0 axis1(2)], 'w')
plot(center(1)+[0 axis2(1)], center(2)+[0 axis2(2)], 'w')
axis equal

% Bruno
Zhiyi Liu
2016-08-29 18:54:03 UTC
Permalink
Dear Bruno,

Thank you so much! Really helpful!

Best,

Zhiyi
Post by Bruno Luong
Post by Zhiyi Liu
Hello Bruno,
Your code is very very helpful! May I please ask a question that if I want to fill in the region of the ellipse (I want to create a binary mask of a ellipse), which part of the code I should modify? Thank you very much!
% Image resolutions
x = 1:100;
y = 1:100;
[X Y] = meshgrid(x,y);
% Ellipse data, axis1 and axis2 are ellipse semi-axis and must be orthogonal
axis1 = [-30.6582563199015; -10.0586237724638];
axis2 = [ -4.42290008808483; 13.48081085893978];
center = [50; 60];
P = [axis1 axis2];
H = P*P';
DX = X(:)-center(1);
DY = Y(:)-center(2);
D = [DX DY]*sqrtm(inv(H));
Z = sum(D.^2,2);
Z = reshape(Z, size(X));
Z = Z <= 1;
% Check
figure
imagesc(Z)
hold on
plot(center(1),center(2),'wo');
plot(center(1)+[0 axis1(1)], center(2)+[0 axis1(2)], 'w')
plot(center(1)+[0 axis2(1)], center(2)+[0 axis2(2)], 'w')
axis equal
% Bruno
ImageAnalyst
2011-03-03 22:02:30 UTC
Permalink
Post by gilad
hello,
I have a binary image (matrix) that i wish to draw an ellipse on.
The eccentricity, major & minor axes, and center are known.
How to do this?
Thanks,
Gilad.
----------------------------------------------------------------
If you have the Image Processing Toolbox, you can use imellipse, like
this:

hEllipse = imellipse(gca,[10 10 50 150]); % Second argument defines
ellipse shape and position.



Here's a full-blown demo program to draw a solid ellipse and a line
both into the overlay above the image, and "burned into" the image.

% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

% Demo to write an ellipse and a line into the overlay of an image,
% and then to burn those overlays into the image.

%----- Initializing steps -----
% Clean up
clc;
clear all;
close all;
workspace; % Display the workspace panel.

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image
Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end

% Display images to prepare for the demo.
monochromeImage = imread('pout.tif');
subplot(2, 4, 1);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 2);
imshow(monochromeImage);
title('Original Image with ellipse in overlay');
subplot(2, 4, 5);
imshow(monochromeImage);
title('Original Image');
subplot(2, 4, 6);
imshow(monochromeImage);
title('Original Image with line in overlay');
set(gcf, 'Position', get(0, 'ScreenSize')); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

%----- Burn ellipse into image -----
% Create elliptical mask, h, as an ROI object over the second image.
subplot(2, 4, 2);
hEllipse = imellipse(gca,[10 10 50 150]); % Second argument defines
ellipse shape and position.
% Create a binary image ("mask") from the ROI object.
binaryImage = hEllipse.createMask();
% Display the ellipse mask.
subplot(2, 4, 3);
imshow(binaryImage);
title('Binary mask of the ellipse');
% Let's try to add some text. (Doesn't work)
% hText = text(50, 100, 'Line of Text');
% textMask = hText.createMask();
% binaryImage = binaryImage & textMask;
% imshow(binaryImage);
% Burn ellipse into image by setting it to 255 wherever the mask is
true.
monochromeImage(binaryImage) = 255;
% Display the image with the "burned in" ellipse.
subplot(2, 4, 4);
imshow(monochromeImage);
title('New image with ellipse burned into image');

%----- Burn line into image -----
burnedImage = imread('pout.tif');
% Create line mask, h, as an ROI object over the second image in the
bottom row.
subplot(2, 4, 6);
hLine = imline(gca,[10 100],[10 100]); % Second argument defines line
endpoints.
% Create a binary image ("mask") from the ROI object.
binaryImage2 = hLine.createMask();
% Display the line mask.
subplot(2, 4, 7);
imshow(binaryImage2);
title('Binary mask of the line');
% Burn line into image by setting it to 255 wherever the mask is true.
burnedImage(binaryImage2) = 255;
% Display the image with the "burned in" line.
subplot(2, 4, 8);
imshow(burnedImage);
title('New image with line burned into image');
Continue reading on narkive:
Loading...