巨大的阵列因为算法不好?

时间:2016-05-09 09:46:20

标签: matlab

我正在尝试实现分割算法,为此我为图像创建空间内核,这取决于到某些预定义图像点的距离(存储在S中)或者更准确我需要任何像素的最小距离在我的图像中预定义的点S.

% Sample S
S = [4 4]; % Normally this would have like 2k entries

% Get Point Coordinates of every matrix position
[cols, rows] = meshgrid(1:width, 1:height);
PtCoords = cat(3, rows, cols);

% Here is the problem. This array gets **HUGE**
SpatDist = zeros(height, width, sumSPts);

% To get The Distance to current S
for j = 1:sumSPts
    currS = S(j, :);
    SCoords = cat(3, repmat(currS(1), height, width),  repmat(currS(2), height, width));

    RowDiff = PtCoords(:,:,1) - SCoords(:,:,1);
    ColDiff = PtCoords(:,:,2) - SCoords(:,:,2);

    SpatDist(:,:,j) = sqrt(RowDiff.^2 + ColDiff.^2);

end

% Save the smallest dist per group
MinSpatDist = zeros(height, width, nSGroups);
SpatKernel = zeros(height, width, sumSPts);
SpatProb = zeros(height, width, nSGroups);
for i = 1:nSGroups;

    % Get Indices of current S
    currIndices = 1:nSPts(i);
    if(i > 1)
        currIndices = currIndices + sum(nSPts(1:i-1));
    end

    % Min over all Scribble Matrices of one group
    MinSpatDist(:,:,i) = min(SpatDist(:,:,currIndices),[], 3);
    Roh = MinSpatDist .* PARAMS.ALPHA;

    % To get normdistr.
    % Get Gaussian pdf value for each pixel   
    for j = currIndices; 
        SpatKernel(:,:,j) = normpdf(SpatDist(:,:,j), 0, Roh(:,:,i));   
    end

    % Sum over all Scribble Matrices of one group
    SpatProb(:,:,i) = sum(SpatKernel(:,:, currIndices), 3);
end

我知道你无法运行代码,但有没有办法用更少的内存使用来做到这一点?

0 个答案:

没有答案