拟合对象的内圆-Matlab [英] Fit an inside circle of an object - Matlab
问题描述
我希望拟合轮廓对象的内圆,该怎么做?在所示的示例中,我试图通过除以MajorAxisLength来计算r,但它不起作用.
I wish to fit an inside circle of an outline object, how can I do it? in the example shown I tried to calculate the r by dividing the MajorAxisLength but it does not work.
代码:
clc;
clear;
RGB = imread('pillsetc.png');
I = rgb2gray(RGB);
bw = imbinarize(I);
imshow(bw)
bw = bwareaopen(bw,30);
bw = imfill(bw,'holes');
imshow(bw)
[B,L] = bwboundaries(bw,'noholes');
stats = regionprops(L,'Centroid','MajorAxisLength');
hold on
k=3;
boundary = B{k};
r = stats(k).MajorAxisLength/2;
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'+');
theta = linspace(0,2*pi);
x = r*cos(theta) + centroid(1);;
y = r*sin(theta) + centroid(2);;
plot(x,y)
axis equal
推荐答案
好的,这是一个近似的解决方案,但是考虑到您可以从regionprops
获得的有限参数,这可能已经足够了.
Ok, this is an approximated solution, but given the limited parameters you can get from regionprops
, this is probably good enough.
我的推导如下:
计算矩形内切圆的半径的目的是估计短边的长度.
the goal to calculate the radius of the inscribed circle of a rectangle is to estimate the length of the short-edge.
如果我们假设与矩形的椭圆拟合产生的面积与矩形近似相同,并且长/短边的比率与短/长轴的比率相同,那么,我们可以得出以下方程式:>
If we assume the elliptic fitting to a rectangle yields approximately the same area as the rectangle, and the short/long edge ratio is the same as the minor/major axis ratios, then, we can obtain the following equation:
x=short edge of the rectangle;
y=long edge of the rectangle;
b=minor axis of the fitted ellipse;
a=major axis of the fitted ellipse;
那么我们就有
x/y=b/a
x*y=a*b*pi
由此,我们可以解决x的值是sqrt(pi)*b
.这样就使内切圆的半径为sqrt(pi)/2*b
.
from that, we can solve the value of x is sqrt(pi)*b
. That makes the radius of the inscribed circle sqrt(pi)/2*b
.
更改下面两行代码
stats = regionprops(L,'Centroid','MinorAxisLength');
...
r = stats(k).MinorAxisLength*(sqrt(pi)/4);
...
我能够接近刻刻的圆圈.试试看.
I was able to get something pretty close to the inscribed circle. Give it a try.
这篇关于拟合对象的内圆-Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!