How to impose Binary mask on rgb color image

33 Ansichten (letzte 30 Tage)
Sarmad Shafique
Sarmad Shafique am 22 Mai 2017
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 Kommentar
Khajista Nizam
Khajista Nizam am 2 Jan. 2018
Bearbeitet: Khajista Nizam am 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);

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 22 Mai 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 Kommentare
Image Analyst
Image Analyst am 10 Okt. 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.
jacques Ben bilombe
jacques Ben bilombe am 11 Jan. 2022
Thank you for the script

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 22 Mai 2017
Bearbeitet: Jan am 22 Mai 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 Kommentare
Jan
Jan am 22 Mai 2017
Is "threshold.jpg" a RGB image?
Marco Andres Acevedo Zamora
Marco Andres Acevedo Zamora am 10 Okt. 2020
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,

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by