[英]Unrecognized function or variable 'f' error
I am new to Matlab and I am trying to implement Hough transform without using Matlab builtin function;我是 Matlab 的新手,我正在尝试在不使用 Matlab 内置 function 的情况下实现霍夫变换; hence I am learning possible ways to do so.
因此,我正在学习可能的方法。 I found a function written in a book that seems to be correct.
我在一本似乎是正确的书中找到了 function。 I got an error and cannot figure it out.
我有一个错误,无法弄清楚。
I want to implement hough transform function and later apply it on to a grayscale image我想实现霍夫变换 function 然后将其应用于灰度图像
% This functions makes use of sparse matrices
function [h, theta, rho] = HT(f, dtheta, drho)
% [H, THETA, RHO] = HOUGH(F, DTHETA, DRHO) computes the hough
% transform of the image F. DTHETA specifies the spacing (in degrees) of
% the the hough transform bins along the theta axis.
% DRHO specifies the spacing of the hough transform bins along the rho
% axis. H is the Hough transform matrix. It is NRHO-by-NTHETA, where
% NRHO = 2*ceil(norm(size(F))/DRHO) - 1, and NTHETA = 2*ceil(90/DTHETA).
if nargin < 3
drho = 1;
end
if nargin < 2
dtheta = 1;
end
f = double(f);
[M,N] = size(f);
theta = linspace(-90, 0, ceil(90/dtheta) + 1);
theta = [theta -fliplr(theta(2:end - 1))];
ntheta = length(theta);
D = sqrt((M - 1)^2 + (N - 1)^2);
q = ceil(D/drho);
nrho = 2*q - 1;
rho = linspace(-q*drho, q*drho, nrho);
[x, y, val] = find(f);
x = x - 1; y = y-1;
%Initialize output.
h = zeros(nrho, length(theta));
% To avoid excessive memory usage, process 1000 nonzero pixel
% values at a time.
for k = 1:ceil(length(val)/1000)
first = (k-1)*1000 + 1;
last = min(first+999, length(x));
x_matrix = repmat(x(first:last), 1, ntheta);
y_matrix = repmat(y(first:last), 1, ntheta);
val_matrix = repmat(val(first:last), 1, ntheta);
theta_matrix = repmat(theta, size(x_matrix, 1), 1)*pi/180;
rho_matrix = x_matrix.*cos(theta_matrix) + ...
y_matrix.*sin(theta_matrix);
slope = (nrho - 1)/(rho(end) - rho(1));
rho_bin_index = round(slope*(rho_matrix - rho(1)) + 1);
theta_bin_index = repmat(1:ntheta, size(x_matrix, 1), 1);
h = h + full(sparse(rho_bin_index(:), theta_bin_index(:), ...
val_matrix(:), nrho, ntheta));
end
%Illustration of Hough transform on a simple binary image
f = zeros(101, 101);
f(1, 1) = 1; f(101, 1) = 1; f(1, 101) = 1;
f(101, 101) = 1; f(51, 51) = 1;
% Compute & display the hough
H = HT(f);
imshow(H, [])
%Label the axis
[H, theta, rho] = HT(f);
imshow(theta, rho, H, [ ], 'notruesize')
axis on, axis normal
xlabel('\theta', ylabel('\rho'))
I expect the hough transform to be displayed using imshow but I get this error我希望使用 imshow 显示霍夫变换,但出现此错误
HT(f, dtheta, drho) Unrecognized function or variable 'f'. HT(f, dtheta, drho) 无法识别 function 或变量“f”。
What you have here is a combination of script and functions in a single.m file.您在这里拥有的是单个.m 文件中的脚本和函数的组合。 This is allowed starting from Matlab R2016b, but the rule is to have the function definitions after the script is finished.
从 Matlab R2016b 开始允许这样做,但规则是在脚本完成后具有 function 定义。
So your code should look like this:所以你的代码应该是这样的:
%Illustration of Hough transform on a simple binary image
f = zeros(101, 101);
f(1, 1) = 1; f(101, 1) = 1; f(1, 101) = 1;
f(101, 101) = 1; f(51, 51) = 1;
% Compute & display the hough
H = HT(f);
imshow(H, [])
%Label the axis
[H, theta, rho] = HT(f);
imshow(theta, rho, H, [ ], 'notruesize')
axis on, axis normal
xlabel('\theta', ylabel('\rho'))
function [h, theta, rho] = HT(f, dtheta, drho)
if nargin < 3
drho = 1;
end
if nargin < 2
dtheta = 1;
end
%.... the rest of the function
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.