作业帮 > 综合 > 作业

matlab直线检测代码报错

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/19 03:10:16
matlab直线检测代码报错
clc;
clear;
i=imread('test3.jpg');
imshow(i)
figure
i=rgb2gray(i);
i_hight=size(i,1);
i_width=size(i,2);
i_edge=edge(i,'robert');
i_hough=zeros(360,480);
for y=1:i_hight
for x=1:i_width
if i(y,x)==0 %坐标系转换
for l=1:360
r=x*cos(l*pi/180)+y*sin(l*pi/180);
w=fix(r)+300; %修正为正整数
%i_hough(w,l)=i_hough(w,l)+1;
end
end
end
end
m=max(max(i_hough));
i_hough=(i_hough./m); %亮度调整
threshold=0.75; %设置直线亮度阈值
ih_hight=size(i_hough,1);
ih_width=size(i_hough,2);
temp=zeros(20,20);
count=0;
for y=1:ih_hight %找那些亮度高的直线
for x=1:ih_width/2
if i_hough(y,x)>threshold
count = count+1;
temp(count,1) = x;
temp(count,2) = y;
end
end
end
imshow(i)
hold on
for i=1:count %画
y=kx+b
k = tan(temp(i,1)*pi/180);
b = (temp(i,2)-300)/sin(temp(i,1)*pi/180);
for j=1:360
x(j)=j;
y(j)=-x(j)/k+b;
end
plot(x,y)
hold on
end
axis equal
figure
imshow(i_hough)
高亮度的直线没有检测,那个大哥修改一下啊
matlab直线检测代码报错
你用我的代码试试
hough变换的直线检测
close all;
clear all;
clc;
%%
I = imread('8_traffic.bmp','bmp');
BW = edge(I,'sobel');
[H,T,R] = hough(BW);
imshow(H,[],'XData',T,'YData',R,...
'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axis on,axis normal,hold on;
P = houghpeaks(H,10,'threshold',ceil(0.3*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
figure(1);
plot(x,y,'s','color','white');
% Find lines and plot them
lines = houghlines(BW,T,R,P,'FillGap',20,'MinLength',40);
figure(2);
[g,t]=edge(I,'sobel',[],'both');
imshow(I);
hold on;
% imshow(g);
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 beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
再问: 试运行了一下,弹出错误: 没有达到突出显示直线的效果 Attempted to access P(:,2); index out of bounds because size(P)=[0,0]. Error in ==> huofushiyan02 at 14 x = T(P(:,2)); y = R(P(:,1)); 出界,能再帮忙改一下吗?
再答: 我运行没问题呀.你的图像是不是有问题,我用的是灰度图像,8位的.