2

I am trying to implement a segmentation algorithm and for that I create a Spatial Kernel for an image which depends on the distance to certain predefined image points (stored in S) or to be more accurate I need the minimum distance of any pixel in my image to predefined points 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

I know you cannot run the code but is there a way to do this with less memory usage?

pfuhlert
  • 533
  • 4
  • 16
  • 1
    your code seems pretty clean... Maybe there are some calculations that you can make without the for cycles. Matlab is great for that – 16per9 May 09 '16 at 10:09
  • Probably you are right. Thats why I'm asking. This first for loop over j returns a Matrix with the minimum distance to every point provided by S which simply contains coordinates. There must be a better way to do this. – pfuhlert May 09 '16 at 10:13
  • Have you tried using `sparse` for the zero matrices? Also, using `single` instead of doubles might save memory, assuming of course that losing some accuracy is ok. – mkfin May 09 '16 at 11:32
  • Because of the N-Dimensional access, this is not an option. e.g. in: N-dimensional indexing allowed for Full matrices only. MinSpatDist(:,:,i) = min(SpatDist(:,:,currIndices),[], 3); – pfuhlert May 09 '16 at 14:34
  • How big is the array `SpatDist`? Does the code make MATLAB crash? Or does it just take long to process? – BillBokeey May 09 '16 at 14:58
  • The sizes I am handling right now is about 700x500 pixels and 2000 Points resulting in an array of 700 milion entries. Matlab does not crash. It fully utilizes my CPU and slows down the PC (probably because no free RAM for other tasks remains) So my RAM is too small to handle this matrix at once which then takes a very long time to compute (~ 20 minutes) – pfuhlert May 10 '16 at 10:18
  • I rephrased the question in http://stackoverflow.com/questions/37138349/get-matrix-of-minimum-coordinate-distance-to-point-set – pfuhlert May 10 '16 at 12:19

0 Answers0