How can I use one plane of RGB as a mask for other planes and generate new image?

조회 수: 1 (최근 30일)
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
Kamran Moradi
Kamran Moradi 2015년 8월 21일
The numbers are the same as you can see in in the command picture, but the result is different, that region is yellow but it is white in masked one. The variable format has been changed also from unit8 to double. Still confusion...

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

채택된 답변

Walter Roberson
Walter Roberson 2015년 8월 21일
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
Kamran Moradi
Kamran Moradi 2015년 8월 21일
편집: Kamran Moradi 2015년 8월 21일
Thank you for your great advises. It Worked.
Original = imread('Image.tif');
[x,y]=size(Original);
% %-------------------------------Red Plane
Red(:,:,1)= Original(:,:,1);
Red(:,:,2)=0;
Red(:,:,3)=0;
% %--------------------------------- Green Plane
Green(:,:,2)= Original(:,:,2);
Green(:,:,3)=0;
% % -- - - - - - - - - -------------- Blue Plane
Blue(:,:,3)= Original(:,:,3);
GreenBW= im2bw(Original(:,:,2));
OriginalMasked = bsxfun(@times, Original, cast(GreenBW, 'like', Original));
imshow(OriginalMasked);
Image Analyst
Image Analyst 2015년 8월 21일
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);
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.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2015년 8월 21일
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
Kamran Moradi
Kamran Moradi 2015년 8월 21일
편집: Kamran Moradi 2015년 8월 21일
Thank you for the answer, but the result is the same as above. The numbers are the same as you can see in in the command picture, but the result is different, that region is yellow but it is white in masked one. Still confusion... This command is faster though and does not have variable type problem

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


Negesse Tadesse
Negesse Tadesse 2019년 7월 31일
does it help for image compresson?
  댓글 수: 1
Image Analyst
Image Analyst 2019년 7월 31일
Does what help with image compression? Masking? It will make the images smaller because much of the image is replaced by a constant number.

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

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by