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:
image mask applied over original image is red?

Subject: image mask applied over original image is red?

From: Cory

Date: 1 Nov, 2010 19:19:03

Message: 1 of 3

Below is my code for what I am trying to accomplish. However, the Map(ROI) is creating a red mask. I would like this mask to be white and not color the image when the mask is applied to the original image.

Any help is appreciated. thanks in advance!

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
close all

message = sprintf('This will allow you to select regions of interest (ROI)');
reply = questdlg(message, 'Run?', 'Continue', 'Cancel','Cancel');
if strcmpi(reply, 'Cancel')
    % User canceled so exit.
    return;
elseif strcmpi(reply, 'Continue')
    I = imread(uigetfile({'*.jpg;*.tif;*.bmp',...
        'All Image Files';'*.*','All Files' }));
end

imshow(I);
Map = false(size(I));

while strcmpi(reply, 'No')== 0
    
    message = sprintf('Do you have any regions to select?');
    reply = questdlg(message,'Continue?', 'Yes', 'No','No');
    
    
    if strcmpi(reply, 'No')
        break;
        
    else
        ROI = roipoly(I);
        Map(ROI) = true;
        continue;
        
    end
    
end

MaskApplied = bsxfun(@times,I,cast(Map,class(I)));

close all
subplot(2,2,1); imshow(I);
subplot(2,2,2); imshow((im2uint8(Map)));
subplot(2,2,3); imshow(MaskApplied);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Subject: image mask applied over original image is red?

From: Sean

Date: 1 Nov, 2010 20:33:04

Message: 2 of 3

"Cory " <cory.hayes@gkndriveline.com> wrote in message <ian3r7$f0k$1@fred.mathworks.com>...
> Below is my code for what I am trying to accomplish. However, the Map(ROI) is creating a red mask. I would like this mask to be white and not color the image when the mask is applied to the original image.
>
> Any help is appreciated. thanks in advance!
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> clear
> close all
>
> message = sprintf('This will allow you to select regions of interest (ROI)');
> reply = questdlg(message, 'Run?', 'Continue', 'Cancel','Cancel');
> if strcmpi(reply, 'Cancel')
> % User canceled so exit.
> return;
> elseif strcmpi(reply, 'Continue')
> I = imread(uigetfile({'*.jpg;*.tif;*.bmp',...
> 'All Image Files';'*.*','All Files' }));
> end
>
> imshow(I);
> Map = false(size(I));
>
> while strcmpi(reply, 'No')== 0
>
> message = sprintf('Do you have any regions to select?');
> reply = questdlg(message,'Continue?', 'Yes', 'No','No');
>
>
> if strcmpi(reply, 'No')
> break;
>
> else
> ROI = roipoly(I);
> Map(ROI) = true;
> continue;
>
> end
>
> end
>
> MaskApplied = bsxfun(@times,I,cast(Map,class(I)));
>
> close all
> subplot(2,2,1); imshow(I);
> subplot(2,2,2); imshow((im2uint8(Map)));
> subplot(2,2,3); imshow(MaskApplied);
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Cory,

I believe the problem you are seeing is that you're setting the mask to be the same size as the RGB image (NxNx3). You really want it to be NxNx1 (i.e. NxN). Thus set:

Map = false(size(I,1),size(I,2)); %It will now be this size

The reason it is red is because you're only turning the first slice (R in RGB) on making the map red.

Good Luck!

-Sean

Subject: image mask applied over original image is red?

From: Cory

Date: 2 Nov, 2010 12:45:04

Message: 3 of 3


> Map = false(size(I,1),size(I,2)); %It will now be this size

Sean,

This works perfectly! thank you for all your help.

Tags for this Thread

No tags are associated with 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