填充图像中的圆形路径 [英] Fill the circular paths in image

查看:103
本文介绍了填充图像中的圆形路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在每张图像中都有很多由圆圈组成的图像(从1到4变化).我试图通过沿圆路径填充丢失的像素来获得清晰的圆图像.

I have many images consisting of circles(varying from 1 to 4) in each image. I am trying to get a clear circle images by filling the missed pixels along the circle path.

我尝试了霍夫变换,但是它的参数是特定于图像的:对于每个图像,我都必须更改参数.由于这个问题,我无法将它们保持在单个for循环中.

I have tried Hough-transform, but its parameters are image specific: for each image I have to change the parameters. With this problem I am unable keep them in a single for loop.

请提供一些方法来执行此操作.谢谢

Please provide some method to do it. Thanks

推荐答案

imfindcircles .但是,该函数假定图像中的圆是完整的",但是在您的示例中,您仅具有圆的(不完整)边界,因此imfindcircles不能直接应用于您的数据.

imfindcircles Does not work
The most "natural" way to approach this problem is to use Matlab's imfindcircles. However, that function assume the circles in the image are "full", yet in your examples you only have the (incomplete) boundaries of the circles, thus imfindcircles cannot be directly applied to your data.

替代方法
您可以使用 ransac 将圆适合数据.一次拟合一个圆至尽可能多的点,当剩下的点太少而根本不适合圆时终止.

Alternative Approach
You can use ransac to fit circles to your data. Fit one circle at a time to as many points as you can, terminate when there are too few points left that fit no circle at all.

要使用 RanSac ,您基本上需要实现两种方法:

To use RanSac you basically need to implement two methods:

  1. 模型拟合方法fitFcn,在给定一点点样本的情况下-在其上画一个圆.

  1. Model fitting method, fitFcn, Given a small sample of your points - fit a circle to them.

距模型方法的距离distFcn,在给定一个圆(模型")的情况下,找到每个点到该圆的距离.

Distance to model method, distFcn, Given a circle ("model") find the distance of each point to that circle.

一旦拥有了这两种方法,RanSac的运行方式大致类似于:
-随机采样很少的点
-使用fitFcn使圆适合这些采样点
-使用distFcn计算所有点到估计圆的距离
-如果有足够的点靠近该圆,则接受该圆并删除属于"该圆的所有点
-如果找不到圆圈或无法解释的"点不足

Once you have these two methods, RanSac operates roughly like:
- randomly sample very few points
- use fitFcn to fit a circle to these sampled points
- use distFcn to compute the distance of all points to estimated circle
- if enough points are close to the circle, accept this circle and remove all point that "belongs" to that circle
- terminate if no circle was found or not enough "unexplained" points

这可以在Matlab中轻松实现.

This can be easily implemented in Matlab.

首先考虑 fitFcn :我们需要一个函数来计算(cxcyr)-2D圆的三个参数(中心和半径).给定一个点(xy),它适合圆iff

First consider fitFcn: we need a function that compute (cx, cy, r) - the three parameters of a 2D circle (center and radii). Given a point (x, y) it fits a circle iff

(x - cx)^2 + (y - cy)^2 = r^2

我们可以按照以下方式将该方程写为已知点(xy)和未知圆(cxcyr)之间的线性关系

We can write this equation as a linear relation between known points (x, y) and unknown circle (cx, cy, r) in the following manner

[-2*x, -2*y, 1] [ cx ; 
                  cy ;                 = [-x^2-y^2]
                  cx^2 + cy^2 - r^2 ]

使用最小二乘估计(与此答案类似),我们可以恢复给定的圆参数圆上有足够的点(至少3个点)

Using a least squares estimation (in a similar manner as in this answer), we can recover the circle parameters given enough points (at least 3) on the circle

这是代码的实际外观

function crc = fit_circle(xy)  % xy is n-by-2 matrix of point coordinates
% fit in least squares sens
x = xy(:, 1);
y = xy(:, 2);
X = [-2*x, -2*y, ones(size(x))]; 
Y = -x.^2 - y.^2;
crc = (X\Y).';  % least squares solution
r2 = -crc(3) +crc(1).^2 + crc(2).^2; 
if r2 <= 0
    crc(3) = 0;
else    
    crc(3) = sqrt(r2);
end
% output crc is a 3 vector (cx, cy, r)

现在我们可以使圆适合点,我们需要使用 distFcn 计算距离,这很简单

Now that we can fit a circle to points, we need to compute the distance using distFcn that is quite simple

function dst = distFcn(crc, xy)
% how good a fit circle for points
x = xy(:, 1) - crc(1);
y = xy(:, 2) - crc(2);
dst = abs(sqrt(x.^2 + y.^2) - crc(3));

将所有内容与matlab的 ransac :

Putting it all together with matlab's ransac:

function circles = find_circles(bw)
% parameters
sample_size = 4;
max_distance = 10;
min_num_points_in_circle = 50;

[y, x] = find(bw > max(bw(:))/2);  % all edges in the image

circles = {};
counter = 0;
while numel(x) > 10 * sample_size && counter < 10
    try
        [circle, inlierIdx] = ransac([x, y], @fit_circle, @distFcn, ...
            sample_size, max_distance);
    catch
        break
    end
    % refit using only inliers
    circle = fit_circle([x(inlierIdx) y(inlierIdx)]);
    dst = distFcn(circle, [x y]);
    founfit = dst < max_distance;
    if sum(founfit) > min_num_points_in_circle
        % this model fits enough points
        circles{end+1} = circle;
        x(founfit) = [];
        y(founfit) = [];
    else
        counter = counter + 1;
    end    
end
circles = vertcat(circles{:});

此函数在您的数据上的输出是(使用 viscircles 绘制圆圈):

And the output of this function on your data is (using viscircles to plot the circles):

这篇关于填充图像中的圆形路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆