如何利用MATLAB实现哈夫变换检测圆
答案:2 悬赏:0 手机版
解决时间 2021-01-31 18:08
- 提问者网友:浩歌待明月
- 2021-01-31 13:17
如何利用MATLAB实现哈夫变换检测圆
最佳答案
- 五星知识达人网友:梦中风几里
- 2021-01-31 13:42
clc,clear all
I = imread('yuan.tif');
[m,n,l] = size(I);
if l>1
I = rgb2gray(I);
end
BW = edge(I,'sobel');
step_r = 1;
step_angle = 0.1;
r_min = 20;
r_max = 30;
thresh = 0.7;
% %%%%%%%%%%%%%%%%%%%%%%%%%%
% input
% BW:二值图像;
% step_r:检测的圆半径步长
% step_angle:角度步长,单位为弧度
% r_min:最小圆半径
% r_max:最大圆半径
% p:阈值,0,1之间的数
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% output
% hough_space:参数空间,h(a,b,r)表示圆心在(a,b)半径为r的圆上的点数
% hough_circl:二值图像,检测到的圆
% para:检测到的圆的圆心、半径
[m,n] = size(BW);
size_r = round((r_max-r_min)/step_r)+1;
size_angle = round(2*pi/step_angle);
hough_space = zeros(m,n,size_r);
[rows,cols] = find(BW);
ecount = size(rows);
% Hough变换
% 将图像空间(x,y)对应到参数空间(a,b,r)
% a = x-r*cos(angle)
% b = y-r*sin(angle)
for i=1:ecount
for r=1:size_r
for k=1:size_angle
a = round(rows(i)-(r_min+(r-1)*step_r)*cos(k*step_angle));
b = round(cols(i)-(r_min+(r-1)*step_r)*sin(k*step_angle));
if(a>0&a<=m&b>0&b<=n)
hough_space(a,b,r) = hough_space(a,b,r)+1;
end
end
end
end
% 搜索超过阈值的聚集点
max_para = max(max(max(hough_space)));
index = find(hough_space>=max_para*thresh );
length = size(index);
hough_circle = false(m,n);
for i=1:ecount
for k=1:length
par3 = floor(index(k)/(m*n))+1;
par2 = floor((index(k)-(par3-1)*(m*n))/m)+1;
par1 = index(k)-(par3-1)*(m*n)-(par2-1)*m;
if((rows(i)-par1)^2+(cols(i)-par2)^2<(r_min+(par3-1)*step_r)^2+5&...
(rows(i)-par1)^2+(cols(i)-par2)^2>(r_min+(par3-1)*step_r)^2-5)
hough_circle(rows(i),cols(i)) = true;
end
end
end
% 打印检测结果
for k=1:length
par3 = floor(index(k)/(m*n))+1;
par2 = floor((index(k)-(par3-1)*(m*n))/m)+1;
par1 = index(k)-(par3-1)*(m*n)-(par2-1)*m;
par3 = r_min+(par3-1)*step_r;
fprintf(1,'Center %d %d radius %d\n',par1,par2,par3);
para(:,k) = [par1,par2,par3];
end
subplot(221),imshow(I),title('原图')
subplot(222),imshow(BW),title('边缘')
subplot(223),imshow(hough_circle),title('检测结果')
I = imread('yuan.tif');
[m,n,l] = size(I);
if l>1
I = rgb2gray(I);
end
BW = edge(I,'sobel');
step_r = 1;
step_angle = 0.1;
r_min = 20;
r_max = 30;
thresh = 0.7;
% %%%%%%%%%%%%%%%%%%%%%%%%%%
% input
% BW:二值图像;
% step_r:检测的圆半径步长
% step_angle:角度步长,单位为弧度
% r_min:最小圆半径
% r_max:最大圆半径
% p:阈值,0,1之间的数
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% output
% hough_space:参数空间,h(a,b,r)表示圆心在(a,b)半径为r的圆上的点数
% hough_circl:二值图像,检测到的圆
% para:检测到的圆的圆心、半径
[m,n] = size(BW);
size_r = round((r_max-r_min)/step_r)+1;
size_angle = round(2*pi/step_angle);
hough_space = zeros(m,n,size_r);
[rows,cols] = find(BW);
ecount = size(rows);
% Hough变换
% 将图像空间(x,y)对应到参数空间(a,b,r)
% a = x-r*cos(angle)
% b = y-r*sin(angle)
for i=1:ecount
for r=1:size_r
for k=1:size_angle
a = round(rows(i)-(r_min+(r-1)*step_r)*cos(k*step_angle));
b = round(cols(i)-(r_min+(r-1)*step_r)*sin(k*step_angle));
if(a>0&a<=m&b>0&b<=n)
hough_space(a,b,r) = hough_space(a,b,r)+1;
end
end
end
end
% 搜索超过阈值的聚集点
max_para = max(max(max(hough_space)));
index = find(hough_space>=max_para*thresh );
length = size(index);
hough_circle = false(m,n);
for i=1:ecount
for k=1:length
par3 = floor(index(k)/(m*n))+1;
par2 = floor((index(k)-(par3-1)*(m*n))/m)+1;
par1 = index(k)-(par3-1)*(m*n)-(par2-1)*m;
if((rows(i)-par1)^2+(cols(i)-par2)^2<(r_min+(par3-1)*step_r)^2+5&...
(rows(i)-par1)^2+(cols(i)-par2)^2>(r_min+(par3-1)*step_r)^2-5)
hough_circle(rows(i),cols(i)) = true;
end
end
end
% 打印检测结果
for k=1:length
par3 = floor(index(k)/(m*n))+1;
par2 = floor((index(k)-(par3-1)*(m*n))/m)+1;
par1 = index(k)-(par3-1)*(m*n)-(par2-1)*m;
par3 = r_min+(par3-1)*step_r;
fprintf(1,'Center %d %d radius %d\n',par1,par2,par3);
para(:,k) = [par1,par2,par3];
end
subplot(221),imshow(I),title('原图')
subplot(222),imshow(BW),title('边缘')
subplot(223),imshow(hough_circle),title('检测结果')
全部回答
- 1楼网友:怀裏藏嬌
- 2021-01-31 14:38
clc
clear
close
%bw=imread('d:\picture\9dafa605d53eea243812bb29.jpg');
rgb=imread('lena.jpg');
bw=rgb2gray(rgb);
thresh=[0.01,0.17];
sigma=2;%定义高斯参数
f = edge(double(bw),'canny',thresh,sigma);
figure(1),
imshow(f,[]);
title('canny 边缘检测');
[h, theta, rho]= hough(f,'rhoresolution', 0.5);
axis on,
axis normal
xlabel('\theta'),ylabel('rho');
p=houghpeaks(h,5);
hold on
lines=houghlines(f,theta,rho,p);
figure,
imshow(f,[]),
title('hough transform detect result'),
hold on
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),'linewidth',4,'color',[.6 .6 .6]);
end
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯