【问题标题】:Calculating the coordinates for the center of a circle in an image计算图像中圆心的坐标
【发布时间】:2012-11-02 07:38:06
【问题描述】:

假设我有这张图片,我想获得 (X , Y) 中每个圆的中心。

MatLab 中有这样的算法吗??

【问题讨论】:

    标签: image algorithm matlab


    【解决方案1】:

    只需拨打regionprops即可:

    img = imread('KxJEJ.jpg');                      % read the image
    imgbw = ~im2bw(img,graythresh(img));            % convert to grayscale
    
    stats  = regionprops(bwlabel(imgbw), 'centroid','area'); % call regionprops to find centroid and area of all connected objects
    area = [stats.Area];                            % extract area
    centre = cat(1,stats.Centroid);                 % extract centroids
    
    centre = centre(area>10,:);                     % filter out dust specks in the image
    

    现在 centre 包含一个 Nx2 数组:第一列是 x 位置,第二列是中心的 y 位置:

    centre =
    
       289.82       451.73
       661.41       461.21
       1000.8       478.01
       1346.7       482.98
    

    【讨论】:

    【解决方案2】:

    这是使用互相关与人工圆作为过滤器的结果。 结果是左上角的[行,列]:

    >> disp(centers)
             483        1347
             460         662
             478        1001
             451         290
    

    没有详细的cmets,请询问。

    im = rgb2gray(im2double(imread('D:/temp/circles.jpg')));
    r = 117; % define radius of circles
    thres_factor = 0.9; % see usage
    %%
    [x, y] = meshgrid(-r : r);
    f = sqrt(x .^ 2 + y .^ 2) >= r;
    %%
    im = im - mean(im(:));
    im = im / std(im(:));
    f = f - mean(f(:));
    f = f / std(f(:)) / numel(f);
    imf_orig = imfilter(im, f, 'replicate');
    %% search local maximas
    imf = imf_orig;
    [n_idx, m_idx] = meshgrid(1 : size(imf, 2), 1 : size(imf, 1));
    threshold = thres_factor * max(imf(:));
    centers = []; % this is the result
    while true
        if max(imf(:)) < threshold
            break;
        end
        [m, n] = find(imf == max(imf(:)), 1, 'first');
        centers = cat(1, centers, [m, n]);
        % now set this area to NaN to skip it in the next iteration
        idx_nan = sqrt((n_idx - n) .^ 2 + (m_idx - m) .^ 2) <= r;
        imf(idx_nan) = nan;
    end
    

    【讨论】:

    • 使用代码的人说它有效,他问“我应该把 plot 命令放在哪里,使用哪个变量?”
    • 这是由figure, imagesc(imf_orig); colorbar; 完成的。显然,经过imf_orig的计算。
    【解决方案3】:

    我记得很多年前在大学里做过这件事!

    我们所做的是应用阈值并使所有内容变为黑白。然后,我们将白色区域(非圆形)涂抹掉,使其散布到圆形上。

    当它们开始消失时,我们有了坐标。

    您也可以在圆周上选取两个点,找到它们之间直线的确切中间,然后通过该点画一条垂直线。如果换行的中间圆的中心。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 2015-03-27
    • 2013-08-07
    • 1970-01-01
    相关资源
    最近更新 更多