Create mask over image

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

Subhadeep Koley
Subhadeep Koley on 15 Feb 2020
Edited: Subhadeep Koley on 15 Feb 2020
You should not use the variable name input as it shadows a MATLAB built-in function. Use the example code below.
close all; clc;
img = im2double(imread('peppers.png'));
figure; imshow(img);
title('Original RGB image');
mask = im2double(imread('peppersMask.png')); % Download the mask attached below
figure; imshow(mask);
title('Mask');
if size(mask, 3) == 1 && size(img, 3) > 1
mask = repmat(mask, [1, 1, size(img, 3)]);
end
CorruptedImage = mask .* img;
Q = mask(:, :, 1);
Red = CorruptedImage(:, :, 1);
Gre = CorruptedImage(:, :, 2);
Blu = CorruptedImage(:, :, 3);
figure; imshow(CorruptedImage);
title('CorruptedImage');
figure; montage(CorruptedImage, 'Size', [1, 3]);
title('R, G, B component of the CorruptedImage');

3 Comments

  1. How did u create that pepperMask image. I can't use it with my images because they are not of the same size.
  2. second point is the mask becomes a 384x512x3 logical. Will it work even if its not the same type as the img?
  3. If u already pre-created the mask do u need this line below?
((im2double(imread('peppersMask.png'))) == 1) % what is the "==1" doing
@ fadams18
1. You can not use the pepperMask image for your image. I created that mask only for giving an example. Also you said that " I already used photoshop to create a mask...". Therefore you can use the mask, which you have created.
2. Yes, it is working as of version R2019b.
3. Actually "==1" is not required. I have upated my answer.
Thank you!

Sign in to comment.

More Answers (1)

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

fadams18
fadams18 on 15 Feb 2020
Edited: fadams18 on 15 Feb 2020
The issue is that im using this for NMF. and i can only apply NMF to each channel and not directly on an RGB image (i guess due to size? )
Question:
  1. your code line means both images must be rgb? Cuz my mask is a greyscale already
  2. after getting the maskedRgbImage, can i still seperate it into the channels?
The main reason why i posted this question is that, my NMF method is not able to reconstruct the original image. Im wondering if it has to do with the way in which i create my mask image and subequenty the maskedRgbImage.
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.
NMF is non-negative matrix factorization. It can also be used for matrix completion. So the idea is to induce the image with missing enries (eg. binary weights or random text over the image) and recover the original image using the NMF algorithm.
I understand it may not be your field.
Im trying to get the image below. In this image, the authors consider the text as the mask. which they add to the original image on the left.
[ Left: Original Image , Right: Corrupted Image ]
So I have atached the original tree image. Now how can i create a mask which is made of text. and replicate the above example.
Then after i achieve this, I will then seperate the corrupted image into the 3 channels since its RGB
Then use NMF to process each channel.
Finally I add all 3 channels back at to get the final reconstructed version of the orignal imge.
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.
I can do the NMF part. what im asking you is if you know how I can reproduce that example above. how can I make that mask and add on the image,
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.
And how are we supposed to know what the mask should be? Thats what i said in the reply. The mask is an image of just text and when added to the image it gives the above image. I just want to know a good way to create this text mask.
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:

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!