Create mask over image

조회 수: 16 (최근 30일)
fadams18
fadams18 2020년 2월 15일
댓글: Image Analyst 2020년 2월 16일
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);

채택된 답변

Subhadeep Koley
Subhadeep Koley 2020년 2월 15일
편집: Subhadeep Koley 2020년 2월 15일
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
Subhadeep Koley
Subhadeep Koley 2020년 2월 15일
@ 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.
fadams18
fadams18 2020년 2월 15일
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 2월 15일
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
fadams18
fadams18 2020년 2월 16일
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.
Image Analyst
Image Analyst 2020년 2월 16일
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:

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by