Error: Edge function: variable might be used before it is defined

1 view (last 30 days)
Hi,
I want to use the edge function and I keep getting an error if I use it inside a function.
Unrecognized function or variable 'edge'.
Error in detectEdge (line 6)
bscan = edge(bscan, 'Canny', canny_threshold);
Error in BugFixingFunctions (line 32)
img = detectEdge(img);
Here is my Code:
function [bscanout] = detectEdge(bscan)
% segment edge
canny_threshold = [0.1, 0.2]; % Anpassen der Schwellenwerte für die Kantenextraktion
bscan = edge(bscan, 'Canny', canny_threshold);
[rows, columns] = size(bscan);
% delete Isle
bscan = bwareaopen(bscan, 150);
temp = zeros(rows,columns);
for i = 1:columns
for j = 1:rows
if bscan(j,i) == 1
temp(j,i) = 1;
break;
end
end
end
temp(350:end,:) = 0;
temp(1:100,:) = 0;
% Interpolate edge Without high peaks
bscanout = temp;
[yValues, xValues] = find(temp);
clear temp;
%if gap is at the start
if xValues(1) ~= 1 && xValues(1) ~= 2
xValues = [1; 2; xValues];
yValues = [yValues(end-4); yValues(end-4); yValues];
elseif xValues(1) ~= 1 && xValues(1) == 2
xValues = [1; xValues];
yValues = [yValues(end-4); yValues];
end
%if gap is at the end
if xValues(end) ~= columns && xValues(end) ~= columns -1
xValues = [xValues; columns - 1; columns];
yValues = [yValues; yValues(3); yValues(3)];
elseif xValues(end) ~= columns && xValues(end) == columns -1
xValues = [xValues; columns];
yValues = [yValues; yValues(3)];
end
%interpolate edge for the missing gap
for i = 1:length(xValues) - 1
if xValues(i) + 1 ~= xValues(i + 1)
edge = interp1(xValues(i-1:i + 1),yValues(i-1:i + 1),1:columns,"pchip");
edge = round(edge);
for j = xValues(i):xValues(i+1)
bscanout(edge(j),j) = 1;
end
end
end
end
If I use the edge function outside the function I wrote, I dont get this error

Accepted Answer

DGM
DGM on 1 Jul 2023
I'm pretty sure the reason that's happening is because later on in the same scope, you're shadowing the function by creating a variable with the same name. The linter assumes that edge is supposed to be a variable, when you're treating it as both. Avoid creating variables that have the same name as functions. If you rename your edge variable, the warning should go away.

More Answers (1)

Image Analyst
Image Analyst on 2 Jul 2023
Try this:
edgeData = interp1(xValues(i-1:i + 1),yValues(i-1:i + 1),1:columns,"pchip");
edgeData = round(edgeData);
for j = xValues(i):xValues(i+1)
bscanout(edgeData(j),j) = 1;
end

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!