Image 如何在MATLAB中在图像上绘制圆?
我在MATLAB中有一个图像:Image 如何在MATLAB中在图像上绘制圆?,image,matlab,plot,geometry,Image,Matlab,Plot,Geometry,我在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); 其中,点是一个与im大小相同的矩阵,其中的点位于感兴趣的点 现在我想在图像上画一个圆圈,在点为1的所有位置 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);
其中,点
是一个与im
大小相同的矩阵,其中的点位于感兴趣的点
现在我想在图像上画一个圆圈,在点
为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轴上的数据,这可能是您使用
x_p
作为函数返回的第一组值的原因。但是,IMSHOW沿y轴显示图像数据的第一个维度,因此FIND返回的第一个值在本例中最终是y坐标值。Hmm我必须在此调用中重新切换它们:
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;
temp\u hull=stats\u single\u object(k).ConvexHull;
对于k2=1:长度(船体温度)
i=i+1;
[x(i,1)]=船体温度(k2,1);
[y(i,1)]=船体温度(k2,2);
结束;
这可能是因为ConvexHull是另一种方式,因此绘图是不同的。或者我犯了一个错误,应该是这样的
[x(i,1)] = temp_hull(k2,2);
[y(i,1)] = temp_hull(k2,1);
[x(i,1)]=船体温度(k2,2);
[y(i,1)]=船体温度(k2,1);
但是,文档中不清楚哪根柱=x或y:
引用:“矩阵的每一行包含多边形一个顶点的x坐标和y坐标。”
我读这篇文章是因为x是第一列,y是第二列。我认为您需要以下方法:
[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)
由Matlab Central的Wang Zhenhai编写的文件交换实现了这一技巧
%----------------------------------------------------------------
% 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
%----------------------------------------------------------------
%----------------------------------------------------------------
%H=圆(中心、半径、NOP、样式)
%此例程绘制一个圆心定义为的圆
%向量中心,半径为缩放半径。没有
%圆上的点数。至于风格,
%使用它的方式与使用rountine绘图的方式相同。
%由于返回了对象的句柄,因此
%使用例程设置以获得最佳结果。
%
%用法示例,
%
%圆圈([1,3],31000,,:');
%圆圈([2,4],21000,'--');
%
%王镇海
%版本1.00
%二○○二年十二月
%----------------------------------------------------------------
在较新版本的MATLAB(我有2013b)中,计算机视觉系统工具箱包含可用于在图像上绘制形状的。以下是从文档中绘制黄色圆圈的示例:
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或更新版本,您可以使用该函数轻松地将圆覆盖在图像上。以下是一个例子:
% 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循环变换的函数。这两个函数的联机文档(上面的链接)都有示例,说明如何在图像中查找圆以及如何在图像上显示检测到的圆
例如:
% 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');
好笑!这里有6个答案,没有一个给出明显的解决方案:
rectangle
函数
从:
通过将“曲率”属性设置为[1]来绘制圆。绘制圆,使其填充点(2,4)和(4,6)之间的矩形区域。Position属性定义包含圆的最小矩形
pos=[2 4 2]代码>
矩形('Position',pos','Curvature',[11])
轴相等
因此,在你的情况下:
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
与公认的答案相反,这些圆圈将随图像缩放,你可以放大,因为它们总是标记整个像素。为了完整起见,你可能还想在代码中添加延迟
。@gnovice:你确定要在绘图
调用中切换x\u p
和y\p
,我在回答中添加了一条注释,解释了为什么需要翻转。缩放图像时,点不会随之缩放-请记住这一点。如何获得值x
和y
?如果您从FIND命令获取它们,则可能需要翻转它们。如果你通过其他方式找到它们,你可能不需要翻转它们。问题是如何在点周围画圆圈,而不是改变点处图像的值。
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