matlab实现霍夫变换检测直线,代码报错,请大神帮忙看看
代码如下:functionmain()clc;clearall;closeall;file='p1.jpg';rgb=imread(file);%读入图片文件bw=im2...
代码如下:
function main()
clc; clear all; close all;
file = 'p1.jpg';
rgb = imread(file); % 读入图片文件
bw = im2bw(rgb); % 二值化
bwe = edge(bw, 'canny'); % canny边缘提取
[H, theta, rho] = hough(bwe); % 霍夫变换
figure; % 显示霍夫变换图像
subplot(2, 1, 1);
imshow(imadjust(mat2gray(H)), [], ...
'XData', theta, 'YData', rho, ...
'InitialMagnification', 'fit');
xlabel('\theta (degrees)'); ylabel('\rho');
title('霍夫变换', 'FontWeight', 'Bold');
axis on; axis normal; hold on;
colormap(hot);
P = houghpeaks(H, 5, 'threshold', ceil(0.3*max(H(:)))); % 峰值提取
x = theta(P(:,2));
y = rho(P(:,1));
plot(x, y, 's', 'Color', 'black', 'MarkerFaceColor', 'b'); % 信息定位
lines = houghlines(bwe, theta, rho, P, 'FillGap', 5, 'MinLength', 7); %霍夫直线
% figure; % 显示图像标记
subplot(2, 1, 2);
imshow(rgb); hold on;
% 下面计算直线交点
lines = [lines lines(1)];
Pts = []; % 存储交点
for k = 1:length(lines)-1
line1 = lines(k);
line2 = lines(k+1);
[px, py] = comput_intersec(line1, line2); % 计算交点
plot(px, py, 'k*');
Pts = [Pts; px py]; % 存储
end
Pts = [Pts; Pts(1, :)];
for k = 1 : size(Pts, 1)-1
plot([Pts(k, 1) Pts(k+1, 1)], [Pts(k, 2) Pts(k+1, 2)], 'k-','LineWidth', 2);
end
title('直线标记', 'FontWeight', 'Bold');
function [px, py] = comput_intersec(line1, line2)
% 计算直线交点,直线为结构体,内容为两个节点信息
p1 = line1.point1; p2 = line1.point2; % 第1条边的两点
p3 = line2.point1; p4 = line2.point2; % 第2条边的两点
x1 = p1(1); y1 = p1(2);
x2 = p2(1); y2 = p2(2);
x3 = p3(1); y3 = p3(2);
x4 = p4(1); y4 = p4(2);
% 计算交点
px = (x1*x3*y2 - x2*x3*y1 - x1*x4*y2 + x2*x4*y1 - x1*x3*y4 + x1*x4*y3 + x2*x3*y4 - x2*x4*y3 )/...
(x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2);
py = (x1*y2*y3 - x2*y1*y3 - x1*y2*y4 + x2*y1*y4 - x3*y1*y4 + x4*y1*y3 + x3*y2*y4 - x4*y2*y3 )/...
(x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2);
报错:
??? Error using ==> hough
Too many input arguments.
Error in ==> hough at 7
[H, theta, rho] = hough(bwe); % 霍夫变换 展开
function main()
clc; clear all; close all;
file = 'p1.jpg';
rgb = imread(file); % 读入图片文件
bw = im2bw(rgb); % 二值化
bwe = edge(bw, 'canny'); % canny边缘提取
[H, theta, rho] = hough(bwe); % 霍夫变换
figure; % 显示霍夫变换图像
subplot(2, 1, 1);
imshow(imadjust(mat2gray(H)), [], ...
'XData', theta, 'YData', rho, ...
'InitialMagnification', 'fit');
xlabel('\theta (degrees)'); ylabel('\rho');
title('霍夫变换', 'FontWeight', 'Bold');
axis on; axis normal; hold on;
colormap(hot);
P = houghpeaks(H, 5, 'threshold', ceil(0.3*max(H(:)))); % 峰值提取
x = theta(P(:,2));
y = rho(P(:,1));
plot(x, y, 's', 'Color', 'black', 'MarkerFaceColor', 'b'); % 信息定位
lines = houghlines(bwe, theta, rho, P, 'FillGap', 5, 'MinLength', 7); %霍夫直线
% figure; % 显示图像标记
subplot(2, 1, 2);
imshow(rgb); hold on;
% 下面计算直线交点
lines = [lines lines(1)];
Pts = []; % 存储交点
for k = 1:length(lines)-1
line1 = lines(k);
line2 = lines(k+1);
[px, py] = comput_intersec(line1, line2); % 计算交点
plot(px, py, 'k*');
Pts = [Pts; px py]; % 存储
end
Pts = [Pts; Pts(1, :)];
for k = 1 : size(Pts, 1)-1
plot([Pts(k, 1) Pts(k+1, 1)], [Pts(k, 2) Pts(k+1, 2)], 'k-','LineWidth', 2);
end
title('直线标记', 'FontWeight', 'Bold');
function [px, py] = comput_intersec(line1, line2)
% 计算直线交点,直线为结构体,内容为两个节点信息
p1 = line1.point1; p2 = line1.point2; % 第1条边的两点
p3 = line2.point1; p4 = line2.point2; % 第2条边的两点
x1 = p1(1); y1 = p1(2);
x2 = p2(1); y2 = p2(2);
x3 = p3(1); y3 = p3(2);
x4 = p4(1); y4 = p4(2);
% 计算交点
px = (x1*x3*y2 - x2*x3*y1 - x1*x4*y2 + x2*x4*y1 - x1*x3*y4 + x1*x4*y3 + x2*x3*y4 - x2*x4*y3 )/...
(x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2);
py = (x1*y2*y3 - x2*y1*y3 - x1*y2*y4 + x2*y1*y4 - x3*y1*y4 + x4*y1*y3 + x3*y2*y4 - x4*y2*y3 )/...
(x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2);
报错:
??? Error using ==> hough
Too many input arguments.
Error in ==> hough at 7
[H, theta, rho] = hough(bwe); % 霍夫变换 展开
东莞大凡
2024-08-07 广告
2024-08-07 广告
在东莞市大凡光学科技有限公司,我们利用Halcon软件处理机器视觉项目时,会用到自定义标定板以满足特定需求。Halcon支持用户根据实际应用场景自定义标定板形状与标记点。这不仅可以灵活应对不同工作环境,还能提高标定精度。通过调整圆点数量、间...
点击进入详情页
本回答由东莞大凡提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询