Hough变换寻找图像(要用canny算子提取图像中的边缘)中的直线和其中最长的段。代码得出的结果不理想。 10
我的代码:clearall;I1=imread('F:\robot\Images\sniper.jpg');I=rgb2gray(I1);%彩色图像灰度化rotI=I;B...
我的代码:
clear all;I1=imread('F:\robot\Images\sniper.jpg'); I=rgb2gray(I1);% 彩色图像灰度化rotI=I;BW=edge(rotI,'canny',0.4);[H,T,R]=hough(BW);figure;imshow(H,[ ],'XData',T,'YData',R,... 'InitialMagnification','fit');xlabel('\theta'),ylabel('\rho');axis on,axis normal,hold on;P =houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));x=T(P(:,2));y=R(P(:,1));plot(x,y,'s','color','white');lines=houghlines(BW,T,R,P,'FillGap',78,'MinLength',5);figure,imshow(BW),hold onmax_len=0;for k=1:length(lines) xy=[lines(k).point1;lines(k).point2]; plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth','2','Color','red'); len=norm(lines(k).point1-lines(k).point2); if(len>max_len) max_len=len; xy_long=xy; endendplot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','r');
原图
结果:
模版是这样的:
是边缘提取得不好的原因吗? 展开
clear all;I1=imread('F:\robot\Images\sniper.jpg'); I=rgb2gray(I1);% 彩色图像灰度化rotI=I;BW=edge(rotI,'canny',0.4);[H,T,R]=hough(BW);figure;imshow(H,[ ],'XData',T,'YData',R,... 'InitialMagnification','fit');xlabel('\theta'),ylabel('\rho');axis on,axis normal,hold on;P =houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));x=T(P(:,2));y=R(P(:,1));plot(x,y,'s','color','white');lines=houghlines(BW,T,R,P,'FillGap',78,'MinLength',5);figure,imshow(BW),hold onmax_len=0;for k=1:length(lines) xy=[lines(k).point1;lines(k).point2]; plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth','2','Color','red'); len=norm(lines(k).point1-lines(k).point2); if(len>max_len) max_len=len; xy_long=xy; endendplot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','r');
原图
结果:
模版是这样的:
是边缘提取得不好的原因吗? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询