代码之家  ›  专栏  ›  技术社区  ›  Nathan Fellman

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

  •  11
  • Nathan Fellman  · 技术社区  · 16 年前

    我在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

    现在我想在图像上画一个圆圈,在所有 要点 是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 功能?

    7 回复  |  直到 16 年前
        1
  •  21
  •   Community Mohan Dere    9 年前

    你可以用普通的 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!
    

    还可以调整打印标记的这些其他特性: MarkerEdgeColor , MarkerFaceColor MarkerSize .

    如果要保存新图像及其上绘制的标记,可以查看 this answer I gave 有关保存图形中的图像时保持图像尺寸的问题。

    注: 使用打印图像数据时 IMSHOW (或 IMAGE 等),行和列的正常解释基本上会发生翻转。通常情况下,数据的第一维度(即行)被认为是位于x轴上的数据,这可能就是您使用 x_p 作为 FIND y轴 ,因此FIND返回的第一个值是 在这种情况下。

        2
  •  2
  •   Nathan Fellman    15 年前

    This file 由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
    %----------------------------------------------------------------
    
        3
  •  1
  •   Community Mohan Dere    6 年前

    rectangle 功能。

    documentation

    通过将“曲率”属性设置为[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
    

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

        4
  •  0
  •   Bill the Lizard    16 年前

    嗯,我不得不在这次通话中重新切换:

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

    引用:“矩阵的每一行包含多边形一个顶点的x坐标和y坐标。”

    我读这篇文章是因为x是第一列,y是第二列。

        5
  •  0
  •   buzjwa    11 年前

    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);
    
        6
  •  0
  •   Bruno Pop-Stefanov    11 年前

    使用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 实现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');
    
        7
  •  -1
  •   Nathan Fellman    16 年前

    以下是我认为您需要的方法:

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