使用matlab计算图像中的圆形对象

时间:2017-02-28 02:41:37

标签: matlab image-processing

如何使用MATLAB计算明亮图像中的圆形物体? 输入图像是: enter image description here

imfindcircles功能在此图片中找不到任何圆圈。

1 个答案:

答案 0 :(得分:2)

基于众所周知的图像处理技术,您可以编写自己的处理工具:

img = imread('Mlj6r.jpg'); % read the image
imgGray = rgb2gray(img); % convert to grayscale
sigma = 1;
imgGray = imgaussfilt(imgGray, sigma); % filter the image (we will take derivatives, which are sensitive to noise)
imshow(imgGray) % show the image
[gx, gy] = gradient(double(imgGray)); % take the first derivative
[gxx, gxy] = gradient(gx); % take the second derivatives
[gxy, gyy] = gradient(gy); % take the second derivatives

k = 0.04; %0.04-0.15 (see wikipedia)
blob = (gxx.*gyy - gxy.*gxy - k*(gxx + gyy).^2); % Harris corner detector (high second derivatives in two perpendicular directions)
blob = blob .* (gxx < 0 & gyy < 0); % select the top of the corner (i.e. positive second derivative)

figure
imshow(blob) % show the blobs

blobThresshold = 1;
circles = imregionalmax(blob) & blob > blobThresshold; % find local maxima and apply a thresshold
figure
imshow(imgGray) % show the original image
hold on
[X, Y] = find(circles); % find the position of the circles
plot(Y, X, 'w.'); % plot the circle positions on top of the original figure
nCircles = length(X)

这段代码计算了2710个圈子,这可能是一个轻微的(但不是很糟糕)高估。

下图显示原始图像,圆圈位置显示为白点。在对象的边界处进行了一些错误的检测。您可以尝试对常量sigmakblobThresshold进行一些调整,以获得更好的结果。特别是,较高的k可能是有益的。有关Harris角点检测器的更多信息,请参阅wikipedia

Original image with the circle position indicates as white dots.