Vectorizing nested for loops
Show older comments
Hi,
I am having a few issuing trying to vectorise multiple nested for loops. This application is video and i am looping through each pixel in each line of each frame of all the frames.
for l=1:nFrames
currentVFrame = read(liveVid, l); % Read in next frame
alphaVFrame = zeros(vidHeight, vidWidth, 3, 'uint8'); % create and then reset to 0
for k = 1:3
for i=1:vidHeight
for j=1:vidWidth
if(currentVFrame(i,j,k) == 254) % 254 = green screen
alphaVFrame(i,j,k) = 000; % Set to transparent instead
else
alphaVFrame(i,j,k) = currentVFrame(i,j, k); % Keep original file info
end
end
end
end
%imshow(alphaVFrame); % Show a preview of trhe current frame
mov(l).cdata = im2uint8(alphaVFrame); % Write the frame to our mov structure (int)
writeVideo(OutputVideo, alphaVFrame); % write the frame to the video file (ext)
end
[i,j] = ndgrid(1:vidHeight*vidWidth);
r = reshape(a(i)+a(j), [nFrames, 3, VidHeight, VidWidth]);
But I get an error:
Maximum variable size allowed by the program is exceeded.
Error in ndgrid (line 63)
varargout{1} = x(:,ones(size(y)));
Can anyone point me in the right direction? I'm using Matlab R2013a.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!