matlab直线检测代码报错clc;clear;i=imread('test3.jpg'); imshow(i)figurei=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_hightfor x=1:i_widthif i(y,x)==0 %坐标系转换for l=1:360r=x
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 06:25:53
matlab直线检测代码报错clc;clear;i=imread('test3.jpg'); imshow(i)figurei=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_hightfor x=1:i_widthif i(y,x)==0 %坐标系转换for l=1:360r=x
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直线检测代码报错clc;clear;i=imread('test3.jpg'); imshow(i)figurei=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_hightfor x=1:i_widthif i(y,x)==0 %坐标系转换for l=1:360r=x
你用我的代码试试
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');