Error: Edge function: variable might be used before it is defined
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
DGM
2023년 7월 1일
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.
추가 답변 (1개)
Image Analyst
2023년 7월 2일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!