Matlab Loop Vectorization

Is there any way to vectorize such a for loop in MATLAB? It's taking a lot of time to execute.

for i = 1:numberOfFrames-1
    frameDifferencesEroded(:,:,i+1) = imabsdiff(frameDifferencesErodedTemp(:,:,i+1),frameDifferencesErodedTemp(:,:,1));
    for k=1:numel(frameDifferences(1,:,i))
        for m=1:numel(frameDifferences(:,1,i))
            if(frameDifferencesEroded(m,k,i+1)>thresold)
                frameDifferences(m,k,i+1) = 255;
            else
                frameDifferences(m,k,i+1) = 0;
            end
        end
    end
end

Assuming you want frameDifferencesEroded(:,:,1) and frameDifferences(:,:,1) to be all zeros, as you are not inputting values into those with your code, this might work for you -

%// Replace imabsdiff with abs(bsxfun(@minus..)), which might be faster
frameDifferencesEroded = abs(bsxfun(@minus,frameDifferencesErodedTemp, frameDifferencesErodedTemp(:,:,1)))

%// Get the thresholding done next
frameDifferences = (frameDifferencesEroded>thresold).*255
我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章