如何在MATLAB中在图像上绘制圆圈?

问题描述 投票:11回答:7

我在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函数的圆圈?

image matlab plot geometry
7个回答
20
投票

你可以使用正常的PLOT命令和circular marker point

[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!

您还可以调整绘图标记的其他属性:MarkerEdgeColorMarkerFaceColorMarkerSize

如果您希望保存新图像并在其上绘制标记,则可以查看this answer I gave以了解在保存图像时保持图像尺寸的问题。

注意:使用IMSHOW(或IMAGE等)绘制图像数据时,行和列的正常解释基本上会被翻转。通常,数据的第一维(即行)被认为是位于x轴上的数据,这可能是您使用x_p作为FIND函数返回的第一组值的原因。但是,IMSHOW沿y轴显示图像数据的第一维,因此在这种情况下,FIND返回的第一个值最终为y坐标值。


2
投票

来自Matlab Central文件交换的Zhenhai Wang的This file可以解决这个问题。

%----------------------------------------------------------------
% 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 <[email protected]>
%   Version 1.00
%   December, 2002
%----------------------------------------------------------------

1
投票

滑稽!这里有6个答案,没有给出明显的解决方案:rectangle函数。

来自documentation

通过将Curvature属性设置为[1 1]来绘制圆。绘制圆圈,使其填充点(2,4)和(4,6)之间的矩形区域。 Position属性定义包含圆的最小矩形。

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

与接受的答案相反,这些圆圈将随图像缩放,您可以放大它们将始终标记整个像素。


0
投票

嗯,我不得不在这个电话中重新切换它们:

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!

In reply to the comments:

使用以下代码创建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);

但是文档不清楚哪个列= x OR y:Quote:“矩阵的每一行包含多边形的一个顶点的x坐标和y坐标。”

我读到这个,因为x是第一列,y是第二列。


0
投票

在较新版本的MATLAB(我有2013b)中,计算机视觉系统工具箱包含vision.ShapeInserter System object,可用于在图像上绘制形状。以下是从文档中绘制黄色圆圈的示例:

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);

0
投票

使用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');

另外,查看实现Hough循环变换的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');

-1
投票

这是我认为你需要的方法:

[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) 
© www.soinside.com 2019 - 2024. All rights reserved.