Create mask over image
Show older comments
I am trying to create mask over an image (input). I already used photoshop to create a mask (which is basiclly an image with same size as input image) t which I add to the original image. Then I use NMF to reconstruct the original image. but it doesnt seem to be working. Am I doing the masking correctly? Any other ideas?
Input=im2double(imread('dataset/3.jpg')); % RGB original image
mask = double( mat2gray( im2double(imread('dataset/mask1.png')) ) == 1 ); % loading a grayscale image icreated in photoshop
if size(mask,3)==1 && size(input,3)>1
mask = repmat(mask,[1,1,size(input,3)]);
end
CorruptedImage = mask.*Input
Q=mask(:,:,1);
Red=CorruptedImage(:,:,1);
Gre=CorruptedImage(:,:,2);
Blu=CorruptedImage(:,:,3);
Accepted Answer
More Answers (1)
Image Analyst
on 15 Feb 2020
To mask an image with a binary image, which will blacken the image outside where the mask is true:
% Method to multiplication channel by channel.
% Mask the image using bsxfun() function to multiply the mask by each color channel or slice individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
8 Comments
Image Analyst
on 15 Feb 2020
I don't know what NMF is or stands for. My code does not require mask to be of any class, because it casts the class to match that of rgbImage. All it requires is that mask is a 2-D image with values of 0 and 1 (if you want to get masking - it will actually work with any values it will just do the multiplication and what you get is what you get.)
You can use the code below to get each color channel into its own variable:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
If you need more help, attach your data in PNG or MAT files along with amocked-up screenshot of what you'd like to obtain as an output.
fadams18
on 15 Feb 2020
Image Analyst
on 15 Feb 2020
OK, good luck. To recombine the repaired/reconstructed images, you can use cat():
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
You may need to use gray2mat() and uint8() to get back into a 24 bit RGB image for display.
fadams18
on 15 Feb 2020
Image Analyst
on 15 Feb 2020
And how are we supposed to know what the mask should be? How about a threshold on the green channel (no idea - just grasping at straws here)
mask = rgbImage(:, :, 2) > 128; % or whatever...
I assumed you somehow already had the mask from the NMF processing you did, because I certainly don't know what it is.
fadams18
on 16 Feb 2020
Image Analyst
on 16 Feb 2020
You said that the authors already had the mask and that they added it to the original image on the left. So why don't you just type up something in a word processor program then type alt-PrintScreen (in Windows) or use the snipping tool to capture a screenshot into a file?
Or Google "text image" and download one:

Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!