Matlabの画像に円を描くにはどうすればよいですか?
質問
Matlabに画像があります:
im = rgb2gray(imread('some_image.jpg');
% normalize the image to be between 0 and 1
im = im/max(max(im));
そして、私はいくつかの処理を行いました。
points = some_processing(im);
どこ points
同じサイズのマトリックスです im
興味深い点にあるものと。
今、私はすべての場所で画像に円を描きたいです points
1です。
Matlabにこれを行う機能はありますか?私が思いつくことができる最高のものは次のとおりです。
[x_p, y_p] = find (points);
[x, y] = meshgrid(1:size(im,1), 1:size(im,2))
r = 5;
circles = zeros(size(im));
for k = 1:length(x_p)
circles = circles + (floor((x - x_p(k)).^2 + (y - y_p(k)).^2) == r);
end
% normalize circles
circles = circles/max(max(circles));
output = im + circles;
imshow(output)
これはやや寛容ではないようです。に似た円を描く方法はありますか line
関数?
解決
通常のものを使用できます プロット でコマンド 円形マーカーポイント:
[x_p,y_p] = find(points);
imshow(im); %# Display your image
hold on; %# Add subsequent plots to the image
plot(y_p,x_p,'o'); %# NOTE: x_p and y_p are switched (see note below)!
hold off; %# Any subsequent plotting will overwrite the image!
プロットマーカーのこれらの他のプロパティを調整することもできます。 MarkerEdgeColor
, MarkerFaceColor
, MarkerSize
.
マーカーがプロットされた状態で新しい画像を保存したい場合は、 私が与えたこの答え 図から画像を保存するときに画像の寸法を維持することについての質問に。
ノート: で画像データをプロットするとき imshow (また 画像, など)、行と列の通常の解釈は本質的に反転します。通常、データの最初の次元(つまり、行)はx軸にあるデータと考えられており、おそらく使用する理由です x_p
によって返される値の最初のセットとして 探す 関数。ただし、imshowは画像データの最初の次元を表示します Y軸, 、したがって、Findによって返された最初の値は最終的に Y座標値 この場合。
他のヒント
このファイル Matlab CentralのファイルExchangeのZhenhai Wangによってトリックがあります。
%----------------------------------------------------------------
% H=CIRCLE(CENTER,RADIUS,NOP,STYLE)
% This routine draws a circle with center defined as
% a vector CENTER, radius as a scaler RADIS. NOP is
% the number of points on the circle. As to STYLE,
% use it the same way as you use the rountine PLOT.
% Since the handle of the object is returned, you
% use routine SET to get the best result.
%
% Usage Examples,
%
% circle([1,3],3,1000,':');
% circle([2,4],2,1000,'--');
%
% Zhenhai Wang <zhenhai@ieee.org>
% Version 1.00
% December, 2002
%----------------------------------------------------------------
面白い!ここには6つの答えがありますが、明らかな解決策を与えるものはありません。 rectangle
関数。
から ドキュメンテーション:
曲率プロパティを[1 1]に設定して円を描きます。円を描いて、ポイント(2,4)と(4,6)の間の長方形の領域を満たすようにします。位置プロパティは、円を含む最小の長方形を定義します。
pos = [2 4 2 2];
rectangle('Position',pos,'Curvature',[1 1])
axis equal
だからあなたの場合:
imshow(im)
hold on
[y, x] = find(points);
for ii=1:length(x)
pos = [x(ii),y(ii)];
pos = [pos-0.5,1,1];
rectangle('position',pos,'curvature',[1 1])
end
受け入れられている答えとは対照的に、これらの円は画像とともにスケーリングされ、常にピクセル全体をマークすることができます。
うーん、私はこの呼び出しでそれらを再スイッチする必要がありました:
k = convhull(x,y);
figure;
imshow(image); %# Display your image
hold on; %# Add subsequent plots to the image
plot(x,y,'o'); %# NOTE: x_p and y_p are switched (see note below)!
hold off; %# Any subsequent plotting will overwrite the image!
コメントへの返信で:
xとyは、次のコードを使用して作成されます。
temp_hull = stats_single_object(k).ConvexHull; for k2 = 1:length(temp_hull) i = i+1; [x(i,1)] = temp_hull(k2,1); [y(i,1)] = temp_hull(k2,2); end;
convexhullが逆であるため、プロットが異なる可能性があります。または私が間違いを犯し、それは
[x(i,1)] = temp_hull(k2,2); [y(i,1)] = temp_hull(k2,1);
ただし、ドキュメントは、どのcolum = xまたはy:Quoteについて明確ではありません。
xは最初の列であり、yは2番目の列であるため、これを読みます。
MATLABの新しいバージョン(2013bがあります)には、コンピュータービジョンシステムツールボックスが含まれています。 vision.ShapeInserter
システムオブジェクト 画像の形を描画するために使用できます。ドキュメントから黄色の円を描く例は次のとおりです。
yellow = uint8([255 255 0]); %// [R G B]; class of yellow must match class of I
shapeInserter = vision.ShapeInserter('Shape','Circles','BorderColor','Custom','CustomBorderColor',yellow);
I = imread('cameraman.tif');
circles = int32([30 30 20; 80 80 25]); %// [x1 y1 radius1;x2 y2 radius2]
RGB = repmat(I,[1,1,3]); %// convert I to an RGB image
J = step(shapeInserter, RGB, circles);
imshow(J);
MATLABおよび画像処理ツールボックスR2012A以下を使用すると、 viscircles
画像上でサークルを簡単にオーバーレイする機能。これが例です:
% Plot 5 circles at random locations
X = rand(5,1);
Y = rand(5,1);
% Keep the radius 0.1 for all of them
R = 0.1*ones(5,1);
% Make them blue
viscircles([X,Y],R,'EdgeColor','b');
また、をチェックしてください imfindcircles
ハフの円形変換を実装する関数。両方の関数(上記のリンク)のオンラインドキュメントには、画像内の円を見つける方法と、画像上に検出された円を表示する方法を示す例があります。
例えば:
% Read the image into the workspace and display it.
A = imread('coins.png');
imshow(A)
% Find all the circles with radius r such that 15 ≤ r ≤ 30.
[centers, radii, metric] = imfindcircles(A,[15 30]);
% Retain the five strongest circles according to the metric values.
centersStrong5 = centers(1:5,:);
radiiStrong5 = radii(1:5);
metricStrong5 = metric(1:5);
% Draw the five strongest circle perimeters.
viscircles(centersStrong5, radiiStrong5,'EdgeColor','b');
これが私があなたが必要とすると思う方法です:
[x_p, y_p] = find (points);
% convert the subscripts to indicies, but transposed into a row vector
a = sub2ind(size(im), x_p, y_p)';
% assign all the values in the image that correspond to the points to a value of zero
im([a]) = 0;
% show the new image
imshow(im)