Hello everyone.
I am trying to make a famous Split and merge algorithm on MATLAB for line extraction.
The error is Out of memory. The likely cause is an infinite recursion within the program.
Error in task1>split (line 88)
[alpha1, r1, idx1] = split(x, y, start, splitPos+start-2, threshold);
I know this question has been asked earlier but Being a beginner in MATLAB, I do not understand how program is going to infinite recursion and how can I avoid it ?? Can anyone tell me the mistake ?? The code is given below.
clc;
close all;
clear all;
threshold = 50;
M = csvread('data.csv');
[m, n] = size(M);
lst = int64(n/6);
for i = 1: lst
index = 6*i-4;
rho(i) = M(1,index);
end
theta = zeros(1, lst);
max_angle = (7*pi)/6;
min_angle = -pi/6;
angle_diff = (max_angle - min_angle)/double(lst);
angle = max_angle;
for i = 1:16
theta(1,i) = angle;
angle = angle - angle_diff;
end
angle = min_angle;
for j = lst:-1:17
theta(1,j) = angle;
angle = angle + angle_diff;
end
%------------------------- Polar Coordiates Done----------------------
[x,y] = pol2cart(theta, rho);
x = -x;
plot(x,y,'o');
N = length(x);
[alpha, r, idx] = split(x,y,1, N,threshold);
function [alpha, r, idx] = split(x,y,start, ending, threshold)
N = ending - start + 1;
delX = x - (sum(x)/N);
delY = y - (sum(y)/N);
num = -2 * sum(delX.*delY);
denom = sum(delY.*delY - delX.*delX);
alpha = atan2(num, denom) / 2;
% compute parameter r by inserting the controid into the line equation and solve for r
r = ((sum(x)/N) * cos(alpha)) + ((sum(y)/N) * sin(alpha));
%-----------------------Line fit done---------------------------------
cosA = cos(alpha);
sinA = sin(alpha);
N = size(x,2);
d = zeros(N,1);
xcosA = x * cosA;
ysinA = y * sinA;
d = xcosA + ysinA - r;
%-------------------------- Distance calculated ----------------------
farOnPositiveSideB = d > threshold;
farOnNegativeSideB = d < -threshold;
neigborsFarAwayOnTheSameSideI = find((farOnPositiveSideB(1:N-1) & farOnPositiveSideB(2:N)) | (farOnNegativeSideB(1:N-1) & farOnNegativeSideB(2:N)));
if isempty(neigborsFarAwayOnTheSameSideI)
splitPos = -1;
else
absDPairSum = abs(d(neigborsFarAwayOnTheSameSideI)) + abs(d(neigborsFarAwayOnTheSameSideI+1));
[ans, splitPos] = max(absDPairSum);
splitPos = neigborsFarAwayOnTheSameSideI(splitPos);
if abs(d(splitPos)) <= abs(d(splitPos + 1)), splitPos = splitPos + 1; end
end
% ------------------------ Split Position calculated --------------------
if (splitPos ~= -1) % found a splitting point
[alpha1, r1, idx1] = split(x, y, start, splitPos+start-2, threshold);
[alpha2, r2, idx2] = split(x, y, splitPos+start-1, ending, threshold);
alpha = [alpha1; alpha2];
r = [r1; r2];
idx = [idx1; idx2];
else % no need to split
idx = [start, ending];
end
return
end

 채택된 답변

Image Analyst
Image Analyst 2019년 3월 8일

0 개 추천

In your split routine, at this line
[alpha1, r1, idx1] = split(x, y, start, splitPos+start-2, threshold);
you cal split() again. So it goes deeper in. And then it geta called again so it goes even deeper. And deeper and deeper forever until it runs out of memory. Your exit strategy before that line, where you set splitPos to -1 (so it will back itself out) evidently never happens, so it just keeps going deeper.
You should set a breakpoint in the routine to try to figure out why splitPos never gets assigned to -1.

댓글 수: 1

hafizas101
hafizas101 2019년 3월 8일
Yes. Thank you very much. When I increased threshold to very large, splitPos became -1 and further working on code also made me realize few other mistakes. thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2019년 3월 8일

댓글:

2019년 3월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by