Search code examples
imagematlabmaskroiimage-masking

Converting location into binary masks and filtering the image with such masks


Say that we have an image which pixels were labeled 1 and 2. How can we do the following in MATLAB?

  • Convert the locations of 1s and 2s into binary masks
  • Filter the image with those masks

Thanks.


Solution

  • Example:

    % some RGB image
    img = im2double(imread('peppers.png'));
    [h,w,~] = size(img);
    
    % lets create some random labels. I'm simply dividing into two halves,
    % where upper half is labeled 1, and lower half labeled 2
    labels = [ones(h/2,w)*1; ones(h/2,w)*2];
    
    % compute masks and filter image using each
    img1 = bsxfun(@times, img, labels==1);
    img2 = bsxfun(@times, img, labels==2);
    
    % show result
    subplot(121), imshow(img1)
    subplot(122), imshow(img2)
    

    images