How to impose Binary mask on rgb color image

36 views (last 30 days)
Hi i have threshold a rgb color image into binary image.
Now i want to impose this image onto my original image to get the purple cells and rest of the image remains black. Anyone have idea? Thanks a lot.
  1 Comment
Khajista Nizam
Khajista Nizam on 2 Jan 2018
Edited: Khajista Nizam on 2 Jan 2018
rgbimage=imread('original image'); %upload your original image b=binarymask; %Your binary mask
%Change the dimension of the binary mask to match with the original image >>bIn3Dims = repmat(b,1,1,3); R = rgbimage(:, :, 1); G = rgbimage(:, :, 2); B = rgbimage(:, :, 3);
%zero will keep the background black. Write 255 if you want it to be white R(~b) = 0; G(~b) = 0; B(~b) = 0; blackBG = cat(3, R, G, B); figure; imshow(blackBG);

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 22 May 2017
To mask an RGB image with a binary/logical image, simply do this:
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
  7 Comments
Image Analyst
Image Analyst on 10 Oct 2020
Khan, Canberk, Sarmad, and others: Attached is a full demo. Adapt as needed.
% Demo to find buns leaving the oven. By Image Analyst, Sep 26, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'cells.jpeg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% It's not an RGB image! It's an indexed image, so read in the indexed image...
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
mask = imread('mask.jpeg');
[rowsm, columnsm, numberOfColorChannelsm] = size(mask)
mask = mask(:,:,1) > 128; % Convert to binary.
if rows ~= rowsm || columns ~= columnsm
% Resize mask to match image.
mask = imresize(mask, [rows, columns], 'Nearest');
end
% Display the initial mask image.
subplot(2, 2, 2);
imshow(mask, []);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the final masked image.
subplot(2, 2, 3);
imshow(maskedRgbImage, []);
axis('on', 'image');
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Display the final masked image of the background by inverting the mask.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
backgroundImage = bsxfun(@times, rgbImage, cast(~mask, 'like', rgbImage));
subplot(2, 2, 4);
imshow(backgroundImage, []);
axis('on', 'image');
title('Background Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.

Sign in to comment.

More Answers (1)

Jan
Jan on 22 May 2017
Edited: Jan on 22 May 2017
RGB = rand(640, 480, 3); % Test data
mask = rand(640, 480) > 0.2;
mask3 = cat(3, mask, mask, mask);
RGBm = RGB;
RGBm(mask3) = 0;
  3 Comments
Jan
Jan on 22 May 2017
Is "threshold.jpg" a RGB image?
Marco Andres Acevedo Zamora
Hello, for a more personalised pixel color on the replacement use (adjust the code to your variable names):
bw = (slide_clahe_sp(:, :, 1) == 255) | ...
(slide_clahe_sp(:, :, 2) == 255) | ...
(slide_clahe_sp(:, :, 3) == 255);
mask = cat(3, bw, bw, bw);
available = size(bw_oversaturated_red(mask), 1);
v = zeros(available, 1);
intervals = available/3:available/3:available+1;
v(1:intervals(1)) = 255; %R
v(intervals(1):intervals(2)) = 0; %G
v(intervals(2):intervals(3)) = 0; %B
This way, you will have a red pixel whenever the 3 dimensional mask is true. It is useful for highlighting oversaturated pixels like the tool available on the optical microscopy software Element NIS-BR. Cheers,

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!