![](/img/trans.png)
[英]Create a Gray level Mask using Matlab and write the same as a gray level image.
[英]Circle detection from gray level image in MATLAB
我的图像如下所示。 我的目标是检测第二张图像中显示的圆圈。 我用[centers,radii] = imfindcircles(IM,[100 300]);
但它一无所获。
有没有其他方法来检测圆圈? 我怎样才能做到这一点?
原始图片:
圆圈:我用油漆画了它。
这是imfindcircles的替代解决方案。 基本上对图像进行阈值处理,使用磁盘结构元素对其进行扩展,然后在找到边缘后,使用此处的文件交换中使用的circle_hough
算法应用Hough变换来检测圆。
这是代码:
clear
clc
close all
A = imread('CircleIm.jpg');
%// Some pre-processing. Treshold image and dilate it.
B = im2bw(A,.85);
se = strel('disk',2);
C = imdilate(B,se);
D = bwareaopen(C,10000);
%// Here imfill is not necessary but you might find it useful in other situations.
E = imfill(D,'holes');
%// Detect edges
F = edge(E);
%// circle_hough from the File Exchange.
%// This code is based on Andrey's answer here:
%https://dsp.stackexchange.com/questions/5930/find-circle-in-noisy-data.
%// Generate range of radii.
radii = 200:10:250;
h = circle_hough(F, radii,'same');
[~,maxIndex] = max(h(:));
[i,j,k] = ind2sub(size(h), maxIndex);
radius = radii(k);
center.x = j;
center.y = i;
%// Generate circle to overlay
N = 200;
theta=linspace(0,2*pi,N);
rho=ones(1,N)*radius;
%Cartesian coordinates
[X,Y] = pol2cart(theta,rho);
figure;
subplot(2,2,1)
imshow(B);
title('Thresholded image (B)','FontSize',16)
subplot(2,2,2)
imshow(E);
title('Filled image (E)','FontSize',16)
subplot(2,2,3)
imshow(F);hold on
plot(center.x-X,center.y-Y,'r-','linewidth',2);
title('Edge image + circle (F)','FontSize',16)
subplot(2,2,4)
imshow(A);hold on
plot(center.x-X,center.y-Y,'r-','linewidth',2);
title('Original image + circle (A)','FontSize',16)
这给出了以下内容:
您可以使用传递给阈值的参数或扩展参数来查看它如何影响结果。
希望有所帮助!
这是解决此问题的另一种方法。 它不是基于霍夫变换,因为imfindcircles和以前的答案是。
基本上:
HT有时很慢,具体取决于输入数据大小和分辨率。 比较两种方法(HT,非HT)的执行时间可能是有用的。
所提出的方法还能够检测另一形状的对象(非圆形)。
function[circle] = ipl_find_circle(I)
% NOTE: I is assumed to be a grayscale image
% Step 1: Segment image using Otsu´s method
t = graythresh(I);
BW = im2bw(I, t);
% Step 2: Leave just "big" components on binary image
[L, num] = bwlabel(BW);
stats = regionprops(L, 'Area', 'PixelIdxList');
area_vector = [stats(:).Area];
area_vector = sort(area_vector);
threshold_pos = floor(num * 0.98);
threshold = area_vector(threshold_pos);
for i=1:num
if(stats(i).Area < threshold)
BW(stats(i).PixelIdxList) = false;
end
end
% Step 3: Dilate image with a circle of small radius
str = strel('disk', 5);
BW = imdilate(BW, str);
% Step 4: Take component with biggest area as the circle
L = bwlabel(BW);
stats = regionprops(L, 'Area', 'BoundingBox', 'Centroid', 'EquivDiameter');
area_vector = [stats(:).Area];
[max_value, max_idx] = max(area_vector);
soi = stats(max_idx);
% Set output variable
circle = imcrop(I, soi.BoundingBox);
% Display results
radius = soi.EquivDiameter/2;
N = 1000;
theta = linspace(0, 2*pi, N);
rho = ones(1, N) * radius;
[X,Y] = pol2cart(theta, rho);
X = soi.Centroid(1) - X;
Y = soi.Centroid(2) - Y;
figure;
subplot(1,2,1);
imshow(I);
hold on;
plot(X, Y, '-r', 'LineWidth', 2);
title('Original graycale image + circle', 'FontSize', 12)
subplot(1,2,2);
imshow(circle);
title('Circle region', 'FontSize', 12);
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.