Hough变换.doc
文本预览下载声明
%Hough变换?clc;clear;close?all?f=imread(D:\dj.jpg);?
%若是彩色图片转灰度化?if?length(size(f))2?????f=rgb2gray(f);?end?%?
figure(1)?
subplot(121);imshow(f);?
%利用edge函数进行边缘检测?j=edge(f,Sobel);?
subplot(122);imshow(j);?[row,col]=size(j);?pinfang=round((row*row+col*col)^0.5);?A=zeros(2*pinfang,180);??
for?m=1:row?????for?n=1:col?????????if?j(m,n)0?
????????????for?thera=1:180?
????????????????r=thera/180*pi;??%角度转弧度?
????????????????rho=round(m*cos(r)+n*sin(r));%ρ=cosθ+sinθ?????????????????rho=rho+pinfang+1;%-l:l转换到1:2l+1?????????????????A(rho,thera)=A(rho,thera)+1;?????????????end?????????end?????end?end??
[rho,thera]=find(A40);%交点超过60条线的点,ma,na为参数空间的坐标点?nma=length(rho);?for?i=1:nma?????hold?on?
????????m=1:row;?%rho=ma(i)-1;?????????r=thera(i)/180*pi;?
????????n=(rho(i)-pinfang-m*cos(r))/(0.00001+sin(r));???????????????plot(n,m,r);?end
%Hough变换?clc;clear;close?all?f=imread(D:\dj.jpg);?
%若是彩色图片转灰度化?if?length(size(f))2?????f=rgb2gray(f);?end?%?
figure(1)?
subplot(121);imshow(f);?
%利用edge函数进行边缘检测?j=edge(f,Sobel);?
subplot(122);imshow(j);?[row,col]=size(j);?pinfang=round((row*row+col*col)^0.5);?A=zeros(2*pinfang,180);??
for?m=1:row?????for?n=1:col?????????if?j(m,n)0?
????????????for?thera=1:180?
????????????????r=thera/180*pi;??%角度转弧度?
????????????????rho=round(m*cos(r)+n*sin(r));%ρ=cosθ+sinθ?????????????????rho=rho+pinfang+1;%-l:l转换到1:2l+1?????????????????A(rho,thera)=A(rho,thera)+1;?????????????end?????????end?????end?end??
[rho,thera]=find(A40);%交点超过60条线的点,ma,na为参数空间的坐标点?nma=length(rho);?for?i=1:nma?????hold?on?
????????m=1:row;?%rho=ma(i)-1;?????????r=thera(i)/180*pi;?
????????n=(rho(i)-pinfang-m*cos(r))/(0.00001+sin(r));???????????????plot(n,m,r);?end?
clc;clear;??%%?录入图像并显示?
f=imread(D:\dj2.jpg);%读入彩色图像,注意不能使用灰度图像??
f=rgb2gray(f);%将彩色图像转换为灰度图像,?f=im2double(f);?figure();???
subplot(2,2,1);imshow(f);title(原图);?%%?提取图像边缘?[m,n]=size(f);%得到图像矩阵行数m,列数n??for?i=3:m-2???
for?j=3:n-2%处理领域较大,所以从图像(3,3)开始,在(m-2,n-2)结束?l(i,j)=-f(i-2,j)-f(i-1,j-1)-2*f(i-1,j)-f(i-1,j+1)-f(i,j-2)-2*f(i,j-1)+16*
显示全部