在MATLAB中检测图像内的圆形 [英] Detect a circular shape inside image in MATLAB
问题描述
检测图像中这些圆形的最快方法是什么?
半径始终在(80-100mm)之间.背景始终为白色.圆圈将始终居中.
我尝试了.
以下是较大算法中的相关代码
%在下方应用霍夫[累计,马戏团,西拉德] = ...CircularHough_Grd(gR,[89 93],...17.4、13、1);%的执行时间为0.72秒让我们看看我们得到了什么imshow(gR);坚持,稍等;plot(circen(:,1),circen(:,2),'r +');对于ii = 1:size(circen,1)矩形('Position',[circen(ii,1)-cirrad(ii),circen(ii,2)-cirrad(ii),2 * cirrad(ii),2 * cirrad(ii)],...'曲率',[1,1],'edgecolor','b','linewidth',1.5);结尾暂缓
有意义的圆圈是中间的圆圈.
这是我的建议:
1.转换为灰色图像,增强与白色的区别"
gimg = min(img,[],3);
2.去除白色区域的阈值
BW = im2bw(gimg,.4);
3.获取图像区域的面积和质心性
st = regionprops(〜BW,'Area','Centroid','PixelIdxList');
4.只选择足够大的区域
sel = [st.Area]>编号(BW)* 0.025;%至少为图像尺寸的2.5%st = st(sel);
5.计算区域到图像中心的距离
cntr = 0.5 * [size(BW,2)size(BW,1)];%X-Y坐标和NOT Row/Cold = sqrt(sum(bsxfun(@ minus,vertcat(st.Centroid),cntr).^ 2,2));
6.选择最靠近中心的区域
[mn idx] = min(d);
7.创建一个面具
res = false(size(BW));res(st(idx).PixelIdxList)= true;
您还可以考虑使用其他区域属性(例如'Eccentricity'
)来更好地拒绝非圆形区域.
Whats the fastest way to detect these circular shapes in a image?
The radius is always between(80-100mm). Background is always white. And the circle will always be in center.
I have tried Hough Transform but I couldn't really get it to work. I am new to this, and I get a feeling like Hough Transform is a overkill for this. Kindly suggest me the right approach to do this.
UPDATE
Here is what I got after applying hough transform.
I have used the algorithm mentioned here.
Following is the relevant code from the bigger algorithm
% applying Hough Below
[accum, circen, cirrad] = ...
CircularHough_Grd(gR, [89 93],...
17.4, 13, 1); % this executes in 0.72 sec
% Lets see what we got
imshow(gR);
hold on;
plot(circen(:,1), circen(:,2), 'r+');
for ii = 1 : size(circen, 1)
rectangle('Position',[circen(ii,1) - cirrad(ii), circen(ii,2) - cirrad(ii), 2*cirrad(ii), 2*cirrad(ii)],...
'Curvature', [1,1], 'edgecolor', 'b', 'linewidth', 1.5);
end
hold off;
The meaningful circle is the one in the middle.
Here's what I propose:
1. convert to gray image, enhancing the "difference from white"
gimg = min( img, [], 3 );
2. Threshold to remove white area
BW = im2bw( gimg, .4 );
3. Get area and centroid porperties of image regions
st = regionprops( ~BW, 'Area', 'Centroid', 'PixelIdxList' );
4. select only large enough regions
sel = [st.Area] > numel(BW)*0.025; % at least 2.5% of image size
st = st(sel);
5. compute region distance to center of image
cntr = .5 * [size(BW,2) size(BW,1)]; % X-Y coordinates and NOT Row/Col
d = sqrt( sum( bsxfun(@minus,vertcat( st.Centroid ), cntr ).^2, 2 ) );
6. pick the region closest to center
[mn idx] = min(d);
7. Create a mask
res = false(size(BW));
res( st(idx).PixelIdxList ) = true;
You might also consider using other region properties (e.g., 'Eccentricity'
) to better reject regions that are not circular.
这篇关于在MATLAB中检测图像内的圆形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!