登录论坛

查看完整版本 : 如何在MATLAB中的图像上画圆?


poster
2019-12-10, 16:49
我在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功能的圆?


回答:
您可以使用带有圆形标记点 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html#Marker)的普通PLOT (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.html)命令:

[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 (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html#MarkerEdgeColor) , MarkerFaceColor (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html#MarkerFaceColor) , MarkerSize (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html#MarkerSize) 。

如果随后要保存新图像并在其上绘制标记,则可以查看这个答案,我回答了 (https://stackoverflow.com/questions/1848176/how-do-i-save-a-plotted-image-and-maintain-the-original-image-size-in-matlab/1848995#1848995)一个有关从图形保存图像时保持图像尺寸的问题。

注意:使用IMSHOW (http://www.mathworks.com/access/helpdesk/help/toolbox/images/imshow.html) (或IMAGE (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/image.html)等)绘制图像数据时,对行和列的常规解释实际上会发生翻转。通常,数据的第一维(即行)被认为是x轴上的数据,这可能就是为什么将x_p用作FIND (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html)函数返回的第一组值的原因。但是,IMSHOW在y轴上显示图像数据的第一维,因此在这种情况下,FIND返回的第一值最终成为y坐标值 。



更多&回答... (https://stackoverflow.com/questions/1860760)