检测出图片中的直线且标记出图片中的最长线段

clc;
clear all;
I=imread('test.jpg');
figure;
imshow(I);
I=rgb2gray(I);
rot1=imrotate(I,33,'crop');  %旋转图像
bw=edge(rot1,'canny');   %使用边缘检测形成二值图像
[H,T,R]=hough(bw);   %生成点的叠加矩阵和坐标T,R
figure;
imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');  %显示统计点的图形
title('变换结果');
xlabel('\theta'),ylabel('\rho');
axis on,axis normal;hold on; %使坐标易于观察
p=houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));  %提取5个极值点,p中为极坐标的位置
x=T(p(:,2));y=R(p(:,1));
plot(x,y,'s','color','blue');
lines=houghlines(bw,T,R,p,'FillGap',5,'MinLength',7);  %生成这五个点在图像中的直线矩阵
figure;
imshow(rot1);,hold on;
max_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','red');  %标出起点和终点
    len=norm(lines(k).point1-lines(k).point2);
    if(len>max_len)  %算出最大的直线
        max_len=len;
        xy_long=xy;
    end
end

plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','Cyan');  %标记出最大的直线






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值