How can I draw ovals with insertshape?

3 views (last 30 days)
Adrian Rio Ristian
Adrian Rio Ristian on 5 Aug 2020
Edited: DGM on 21 Mar 2023
How can I draw ovals with insertshape? I'm trying to draw a face using binaryb images

Answers (1)

DGM
DGM on 20 Mar 2023
Edited: DGM on 21 Mar 2023
According to the documentation for insertShape(), there is no such option for an oval, although I suppose you could consider a circle to be an oval.
Of course, it does support filled polygons, so if you can generate the vertices of the desired oval, you can draw them.
% the base image
sz = [500 500];
outpict = repmat(permute([0.8 0.3 1]*0.3,[1 3 2]),sz);
% face
facecolor = [0.8 0.3 0.2];
[x y] = makeoval(150,[250 200],90,'type','superegg');
outpict = insertShape(outpict,'filled-polygon',[x y],'color',facecolor,'opacity',1);
% ears
[x y] = makeoval([40 70 40],[100 240],80,'type','freeegg','p',[1.5 2.5]);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',facecolor*0.9,'opacity',1);
outpict = insertShape(outpict,'filled-polygon',[sz(1)-x y],'color',facecolor*0.9,'opacity',1);
[x y] = makeoval(140,[250 200],90,'type','superegg');
outpict = insertShape(outpict,'filled-polygon',[x y],'color',facecolor,'opacity',1);
% eyes
eyepos = [190 220];
[x y] = makeoval(20,eyepos-[0 10],0,'type','ellipse','ratio',1.8);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',facecolor*0.9,'opacity',1);
outpict = insertShape(outpict,'filled-polygon',[sz(1)-x y],'color',facecolor*0.9,'opacity',1);
[x y] = makeoval(20,eyepos,0,'type','ellipse','ratio',1.8);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',[0.9 0.9 0.9],'opacity',1);
outpict = insertShape(outpict,'filled-polygon',[sz(1)-x y],'color',[0.9 0.9 0.9],'opacity',1);
[x y] = makeoval(18,eyepos,90,'type','ellipse','ratio',1);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',[0 0 0],'opacity',1);
outpict = insertShape(outpict,'filled-polygon',[sz(1)-x y],'color',[0 0 0],'opacity',1);
% mouth
mouthpos = [250 350];
[x y] = makeoval(20,mouthpos,0,'type','ellipse','ratio',2.5);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',[0.7 0.1 0.1],'opacity',1);
[x y] = makeoval(20,mouthpos-[0 10],0,'type','ellipse','ratio',2.5);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',facecolor,'opacity',1);
% nose
mouthpos = [250 290];
[x y] = makeoval([30 50 30],mouthpos+[5 5],-90,'type','freeegg','p',[1.5 2.5]);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',facecolor*0.5,'opacity',1);
[x y] = makeoval([30 50 30],mouthpos,-90,'type','freeegg','p',[1.5 2.5]);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',facecolor,'opacity',1);
% display mr. creepyface
imshow(outpict)
Of course, if all you actually want is an ellipse, you can use imellipse() or drawellipse() with basic image composition for fill the area.
Alternatively, if you're going to use polygons to draw a face, why use ovals when you can just use arbitrary polygons to draw a face?
% image size
sz = [512 420];
% the color table
CT0 = [99 27 68;
162 70 88;
135 80 132;
190 78 82;
219 117 108;
213 144 128;
201 160 167;
236 196 165];
% color indices for each object
% the first object is the background
CI = [4 2 1 1 2 1 1 5 5 5 6 6 6 8 8 8 6 5 8 2 2 2 2 2 7 7 2 2 3 3 8];
% create base image
outpict = repmat(permute(uint8(CT0(CI(1),:)),[1 3 2]),sz);
% load some vertex data
S = load('facedata.mat');
% draw each polygon in sequence
for k = 1:numel(S.VertexData)
x = S.VertexData{k}(:,1);
y = S.VertexData{k}(:,2);
c = CT0(CI(k+1),:);
outpict = insertShape(outpict,'filled-polygon',[x y],'color',c,'opacity',1);
end
imshow(outpict)
That's much better.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!