How can I use one plane of RGB as a mask for other planes and generate new image?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Kamran Moradi
il 21 Ago 2015
Commentato: Image Analyst
il 31 Lug 2019
Original = imread('Image.tif');
[x,y,n]=size(Original);
% %-------------------------------Red Plane
Red(:,:,2)=zeros(x,y);
Red(:,:,3)=zeros(x,y);
Red(:,:,1)= Original(:,:,1);
%--------------------------------- Green Plane
Green(:,:,3)=zeros(x,y);
Green(:,:,1)=zeros(x,y);
Green(:,:,2)= Original(:,:,2);
% -- - - - - - - - - -------------- Blue Plane
Blue(:,:,2)=zeros(x,y);
Blue(:,:,1)=zeros(x,y);
Blue(:,:,3)= Original(:,:,3);
%-------------------------converts the green planes to Black and White to make mask
GreenBW= im2bw(Original(:,:,2));
OriginalMasked(:,:,1) = GreenBW.*Red(:,:,1);
OriginalMasked(:,:,2) = Green(:,:,2);
OriginalMasked(:,:,3) = GreenBW.*Blue(:,:,3);
imshow(OriginalMasked);
2 Commenti
Risposta accettata
Walter Roberson
il 21 Ago 2015
When you are creating Red, Green, Blue, copy over Original first so that you get the correct datatype for the destination. Currently you are copying it last.
4 Commenti
Image Analyst
il 21 Ago 2015
Be careful about using non-standard definitions for x and y like you did. That usually eventually leads to trouble. Most people would do
[y, x] = size(TwoDMatrix);
Of course you didn't do that, and you did something even more risky or dangerous - you used it on an image read in from imread(). Why is it dangerous to use
[x, y] = size(theImage);
instead of
[rows, columns, numberOfColorChannels] = size(theImage);
(besides just swapping x and y)? Well, read this: http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/
If you don't use a third argument on an image array, and the image array is a color image, the second argument ("x" or "y" as you called it) will actually be the number of columns multiplied by the number of color channels.
Più risposte (2)
Image Analyst
il 21 Ago 2015
An alternate way to mask an RGB image by a logical 2D mask image, promoted by Sean, is this:
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
1 Commento
Negesse Tadesse
il 31 Lug 2019
does it help for image compresson?
1 Commento
Image Analyst
il 31 Lug 2019
Does what help with image compression? Masking? It will make the images smaller because much of the image is replaced by a constant number.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!