Search code examples
matlabraster

How to buffer pixels by their associated pixel values?


What methods can I use to buffer pixels by their associated pixel values? For example, the image on the left shows a raster image with pixel radius values ranging from 0 - 5 (note "black" values are 0). The image on the right shows the buffers I am trying to produce based on these pixel values. Per Stack Exchange policies, I have also included a MATLAB script for reproducible data.

enter image description here


% Generate a grid of 0s to begin with.
m = zeros(300, 400, 'uint8');

% Generate 1000 random pixels.
numRandom = 1000;
linearIndices = randi(numel(m), 1, numRandom);

% Assign a radius value of 1-5 for each pixel
m(linearIndices) = randi(5, [numel(linearIndices) 1]);

% Display it.  
image(m);
colormap(hot); 

Solution

  • One approach:

    % Generate a grid of 0's to begin with.
    m = zeros(300, 400, 'uint8');
    
    % Generate 1000 random pixels.
    numRandom = 9;
    linearIndices = randi(numel(m), 1, numRandom);
    
    % Assign a radius value of 1-5 for each pixel
    m(linearIndices) = randi(5, [numel(linearIndices) 1]);
    
    
    
    %%
    buffer = false(size(m));
    for radius =1:5 % update to actual range
        im_r  = m==radius;
        se    = strel('disk',radius);
        im_rb = imfilter(im_r, double(se.getnhood()));
    
        buffer = buffer | im_rb;
    end
    
    imshowpair(m,buffer,'montage');