How can I draw ovals with insertshape?

조회 수: 3 (최근 30일)
Adrian Rio Ristian
Adrian Rio Ristian 2020년 8월 5일
편집: DGM 2023년 3월 21일
How can I draw ovals with insertshape? I'm trying to draw a face using binaryb images

답변 (1개)

DGM
DGM 2023년 3월 20일
편집: DGM 2023년 3월 21일
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.

카테고리

Help CenterFile Exchange에서 Map Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by