MATLAB中灰度图像的圆检测 [英] Circle detection from gray level image in MATLAB
问题描述
我的图像如下所示。我的目标是检测第二张图像中显示的圆圈。我使用 [centers,radii] = imfindcircles(IM,[100 300]);
但它什么都没找到。
I have the image shown below. My aim is to detect the circle which shown in the second image. I used [centers,radii] = imfindcircles(IM,[100 300]);
but it found nothing.
还有其他方法来检测圆圈吗?我怎么能这样做?
Is there any other way to detect the circle? How can I do that?
原始图片:
Original image:
圆圈:我用油漆画了它。
The circle:I drew it with paint.
推荐答案
这是imfindcircles的另一种解决方案。基本上对图像进行阈值处理,使用磁盘结构元素对其进行扩展,然后在找到边缘后,使用文件交换中可用的 circle_hough
算法应用Hough变换来检测圆圈此处。
Here is an alternative solution to imfindcircles. Basically threshold the image, dilate it with a disk structuring element and then, after finding the edges, apply a Hough transform to detect the circle using the circle_hough
algorithm available form the file exchange here.
以下是代码:
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)
其中给出以下内容:
您可以使用传递给阈值的参数或扩大参数,看看它如何影响结果。
You can play around with the parameters passed to threshold or to dilate as well to see how it affects the outcome.
希望有所帮助!
这篇关于MATLAB中灰度图像的圆检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!