将圆拟合成二进制图像 [英] fitting a circle to a binary image

查看:194
本文介绍了将圆拟合成二进制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用skim age的阈值算法来获取一些二进制掩码。例如,我获得这样的二进制图像:

I have been using skim age's thresholding algorithms to get some binary mask. For example, I obtain binary images like this:

我想弄清楚的是我如何在这个二进制掩码中使用圆圈。限制是圆圈应尽可能多地覆盖白色区域,圆圈的整个圆周应完全位于白色部分。

What I am trying to figure out is how can I fit a circle to this binary mask. The constraint is the circle should cover as much of the white areas as possible and the whole circumference of the circle should lie entirely on the white parts.

我一直在破坏我的关于我如何有效地做到这一点,但没有提出任何有效的解决方案。

I have been wrecking my head on how I can do this efficiently but have come up with no solution that works.

我认为可能有的一种方法是:

One approach I thought that might be something is:


  • 找到一些最优的图像/圆的中心(我不知道该如何做到这一点。也许是一些光栅扫描方法)。

  • 计算圆圈以增加半径并计算它何时开始离开白色区域或图像外。

  • 然后质心和半径将描述圆圈。

推荐答案

这是一个试图通过最小化使最佳圆适合的解决方案。很快就会发现气泡不是一个圆圈:)请注意使用regionprops来轻松确定区域的面积,质心等。

Here is a solution that tries to make an optimal circle fit via minimization. It soon becomes apparent that the bubble isn't a circle :) Note the use of "regionprops" for easily determining area, centroid, etc. of regions.

from skimage import io, color, measure, draw, img_as_bool
import numpy as np
from scipy import optimize
import matplotlib.pyplot as plt


image = img_as_bool(color.rgb2gray(io.imread('bubble.jpg')))
regions = measure.regionprops(image)
bubble = regions[0]

y0, x0 = bubble.centroid
r = bubble.major_axis_length / 2.

def cost(params):
    x0, y0, r = params
    coords = draw.circle(y0, x0, r, shape=image.shape)
    template = np.zeros_like(image)
    template[coords] = 1
    return -np.sum(template == image)

x0, y0, r = optimize.fmin(cost, (x0, y0, r))

import matplotlib.pyplot as plt

f, ax = plt.subplots()
circle = plt.Circle((x0, y0), r)
ax.imshow(image, cmap='gray', interpolation='nearest')
ax.add_artist(circle)
plt.show()

这篇关于将圆拟合成二进制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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