Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Using a binary mask on a RGB image

Subject: Using a binary mask on a RGB image

From: Els

Date: 13 Jan, 2010 18:23:04

Message: 1 of 5

I made a binary mask (double) based on characteristics of a RGB image(uint8).
Now I want to use this mask to show only the pixels of the RGB image where the mask has value 1. I guess this should be very easy, tried several things, but its not working. How can I do this?

Thanks in advance

Subject: Using a binary mask on a RGB image

From: Nathan

Date: 13 Jan, 2010 18:28:17

Message: 2 of 5

On Jan 13, 10:23 am, "Els" <tikva...@msn.com> wrote:
> I made a binary mask (double) based on characteristics of a RGB image(uint8).
> Now I want to use this mask to show only the pixels of the RGB image where the mask has value 1. I guess this should be very easy, tried several things, but its not working. How can I do this?
>
> Thanks in advance

Assume your image is a matrix A (of the same size as the mask)

imagesc(A(mask))

Does this work for you?

-Nathan

Subject: Using a binary mask on a RGB image

From: us

Date: 13 Jan, 2010 18:34:05

Message: 3 of 5

"Els" <tikva_85@msn.com> wrote in message <hil328$dll$1@fred.mathworks.com>...
> I made a binary mask (double) based on characteristics of a RGB image(uint8).
> Now I want to use this mask to show only the pixels of the RGB image where the mask has value 1. I guess this should be very easy, tried several things, but its not working. How can I do this?
>
> Thanks in advance

one of the solutions

     img=ceil(5*rand([3,4,3]))./5; % <- rgb...
     msk=[
           1,0,1,0
           0,1,0,1
           1,1,0,0
     ];
     imm=bsxfun(@times,img,msk);
     imagesc([img;imm]);
     axis image;

us

Subject: Using a binary mask on a RGB image

From: ImageAnalyst

Date: 13 Jan, 2010 19:01:42

Message: 4 of 5

On Jan 13, 1:23 pm, "Els" <tikva...@msn.com> wrote:
> I made a binary mask (double) based on characteristics of a RGB image(uint8).
> Now I want to use this mask to show only the pixels of the RGB image where the mask has value 1. I guess this should be very easy, tried several things, but its not working. How can I do this?
>
> Thanks in advance

-------------------------------------------------------------------------------------------------------
You need to cast your mask to uint8 (because a double array times a
uint8 array is not allowed), and then you can apply the mask one color
plane at a time, like I do in this demo:

% Demo macro to very, very simple color detection in RGB color space
% by ImageAnalyst
clc;
close all;

% Read standard MATLAB demo image.
rgbImage = imread('onion.png');

% Display the original image.
subplot(3, 4, 1);
imshow(rgbImage);
title('Original RGB Image');
% Maximize figure.
set(gcf, 'Position', get(0, 'ScreenSize'));

% Split into color bands.
redBand = rgbImage(:,:, 1);
greenBand = rgbImage(:,:, 2);
blueBand = rgbImage(:,:, 3);

% Display them.
subplot(3, 4, 2);
imshow(redBand);
title('Red band');
subplot(3, 4, 3);
imshow(greenBand);
title('Green band');
subplot(3, 4, 4);
imshow(blueBand);
title('Blue Band');

% Threshold each color band.
redthreshold = 68;
greenThreshold = 70;
blueThreshold = 72;
redMask = (redBand > redthreshold);
greenMask = (greenBand < greenThreshold);
blueMask = (blueBand < blueThreshold);

% Display them.
subplot(3, 4, 6);
imshow(redMask, []);
title('Red Mask');
subplot(3, 4, 7);
imshow(greenMask, []);
title('Green Mask');
subplot(3, 4, 8);
imshow(blueMask, []);
title('Blue Mask');

% Combine the masks to find where all 3 are "true."
redObjectsMask = uint8(redMask & greenMask & blueMask);
subplot(3, 4, 9);
imshow(redObjectsMask, []);
title('Red Objects Mask');
maskedrgbImage = uint8(zeros(size(redObjectsMask))); % Initialize
maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* redObjectsMask;
maskedrgbImage(:,:,2) = rgbImage(:,:,2) .* redObjectsMask;
maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* redObjectsMask;
subplot(3, 4, 10);
imshow(maskedrgbImage);
title('Masked Original Image');

Subject: Using a binary mask on a RGB image

From: Els

Date: 13 Jan, 2010 20:09:04

Message: 5 of 5

This final post did the tric. Thanks a lot.
My fault was that I kept the mask as a double, while I should make it a uint8.
It works great now.

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <6df6be0e-f44f-43f4-a8a7-ebc040ff576e@g25g2000yqd.googlegroups.com>...
> On Jan 13, 1:23 pm, "Els" <tikva...@msn.com> wrote:
> > I made a binary mask (double) based on characteristics of a RGB image(uint8).
> > Now I want to use this mask to show only the pixels of the RGB image where the mask has value 1. I guess this should be very easy, tried several things, but its not working. How can I do this?
> >
> > Thanks in advance
>
> -------------------------------------------------------------------------------------------------------
> You need to cast your mask to uint8 (because a double array times a
> uint8 array is not allowed), and then you can apply the mask one color
> plane at a time, like I do in this demo:
>
> % Demo macro to very, very simple color detection in RGB color space
> % by ImageAnalyst
> clc;
> close all;
>
> % Read standard MATLAB demo image.
> rgbImage = imread('onion.png');
>
> % Display the original image.
> subplot(3, 4, 1);
> imshow(rgbImage);
> title('Original RGB Image');
> % Maximize figure.
> set(gcf, 'Position', get(0, 'ScreenSize'));
>
> % Split into color bands.
> redBand = rgbImage(:,:, 1);
> greenBand = rgbImage(:,:, 2);
> blueBand = rgbImage(:,:, 3);
>
> % Display them.
> subplot(3, 4, 2);
> imshow(redBand);
> title('Red band');
> subplot(3, 4, 3);
> imshow(greenBand);
> title('Green band');
> subplot(3, 4, 4);
> imshow(blueBand);
> title('Blue Band');
>
> % Threshold each color band.
> redthreshold = 68;
> greenThreshold = 70;
> blueThreshold = 72;
> redMask = (redBand > redthreshold);
> greenMask = (greenBand < greenThreshold);
> blueMask = (blueBand < blueThreshold);
>
> % Display them.
> subplot(3, 4, 6);
> imshow(redMask, []);
> title('Red Mask');
> subplot(3, 4, 7);
> imshow(greenMask, []);
> title('Green Mask');
> subplot(3, 4, 8);
> imshow(blueMask, []);
> title('Blue Mask');
>
> % Combine the masks to find where all 3 are "true."
> redObjectsMask = uint8(redMask & greenMask & blueMask);
> subplot(3, 4, 9);
> imshow(redObjectsMask, []);
> title('Red Objects Mask');
> maskedrgbImage = uint8(zeros(size(redObjectsMask))); % Initialize
> maskedrgbImage(:,:,1) = rgbImage(:,:,1) .* redObjectsMask;
> maskedrgbImage(:,:,2) = rgbImage(:,:,2) .* redObjectsMask;
> maskedrgbImage(:,:,3) = rgbImage(:,:,3) .* redObjectsMask;
> subplot(3, 4, 10);
> imshow(maskedrgbImage);
> title('Masked Original Image');

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us