hold on
を使用して画像を表示した後のステートメントのため、既存の図の上に楕円が描画されimshow
ます。したがって、これの代わりに:
imshow(bw)
hold on
figure
次のステートメントを使用して新しい図を作成するだけです。
figure
[編集]
わかりました、まず第一に、保存する(x, y)
と楕円の中心のみが得られます。a
楕円を描くには、楕円/短軸のサイズ ( 、b
) と方向角 ( )も格納する必要がありますtheta
。
私はあなたがすでに持っているループを単に再利用しますが、plot
座標ごとに bw 画像ピクセルを 1 に設定するだけに置き換えます。
% get image dimensions
dim = size(bw);
% preallocate a blank bw image
target = false(dim);
% for each ellipse
for k = 1:length(s)
% this part remains the same:
xbar = s(k).Centroid(1);
ybar = s(k).Centroid(2);
a = s(k).MajorAxisLength/2;
b = s(k).MinorAxisLength/2;
theta = pi*s(k).Orientation/180;
R = [ cos(theta) sin(theta)
-sin(theta) cos(theta)];
xy = [a*cosphi; b*sinphi];
xy = R*xy;
x = xy(1,:) + xbar;
y = xy(2,:) + ybar;
% ----------
% but replace plot(x,y) with this:
% limit to image dimensions (1:256)
x(x<1) = 1; x(x>dim(1))=dim(1);
y(y<1) = 1; y(y>dim(2))=dim(2);
% set those pixels to 1
target(sub2ind(dim, round(x),round(y))) = 1;
end
imshow(target);
現在、画像境界の半分外側にある楕円があります。そのため、x、y 座標を (1:256) に制限する必要があります。そうしないと、範囲外エラーが発生します。これらの楕円を完全に削除するか、ここで行ったように部分的に描画するかを再考する必要があります。