user164013
user164013

Reputation: 133

code to define mask in matlab

I am working on developing a CBIR system, where I have to segment my RGB image in the following way:

Regions I want to divide my Image into

I am implementing the code in matlab, but I am unable to build the proper masks for it. I used imellipse but that requires the image handle which is achieved using imshow, but I don't want to show my image. My code is

img=imread('peppers.png');
h_im=imshow(img); %I want to get rid of imshow because I don't want to show the image

[height, width, planes]=size(img);
%(cX,cY) is image center
cX=width/2;
cY=(height)/2;

%Here I define my ROI which is an ellipse that stretches to 75 percent of
%height and width of the image
e=imellipse(gca,[(1/2-3/8)*width, (1/2-3/8)*height,(3/4)*width,(3/4)*height]);
mymask=createMask(e,h_im);

%extending mask to three channels
mymask=repmat(mymask,[1 1 3]);
ROI=img;
ROI(mymask==0)=0;
figure, imshow(ROI);

The output image

Upvotes: 1

Views: 408

Answers (3)

JimR
JimR

Reputation: 2225

You can generate the ellipse mask yourself rather than using the imellipse command.

% Create a meshgrid the same size of the image in order to generate the mask
[x y] = meshgrid(1:size(img, 1), 1:size(img, 2));

% Create the eclipse mask using the general form of an eclipse
% This will be centered in the middle of the image 
% and have a height and width of 75% of th eimage
A = (0.75/2)*size(img, 2);
B = (0.75/2)*size(img, 1);
mask = A^2*(x - floor(size(img, 1)/2)).^2 + B^2*(y - floor(size(img, 2)/2)).^2<=A^2*B^2;

% Apply the eclipse mask
masked_image = img.*repmat(mask, [1, 1, 3]);

Upvotes: 2

gfkri
gfkri

Reputation: 2179

I think this code is easy to understand and easily adjustable to address an arbitrary part of your image (It has the same output as your code) :

img = imread('peppers.png');

% define the size of your ellipse relatively to the image dimension
factor = 0.75;

hwidth = size(img, 2) / 2.0;
hheight = size(img, 1) / 2.0;

a = hwidth * factor;
b = hheight * factor;

[x, y] = meshgrid(1:hwidth, 1:hheight);

% simple ellipse equation gets us part three of your mask
bottom_right_mask = (((x.^2)/a^2+(y.^2)/b^2)<=1); 

% flip to get the remaining ones
top_right_mask = flipud(bottom_right_mask);
bottom_left_mask = fliplr(bottom_right_mask);
top_left_mask = flipud(bottom_left_mask);

mask = [top_left_mask, top_right_mask; ...
        bottom_left_mask, bottom_right_mask];

multichannel_mask = repmat(mask,[1 1 3]);

ROI = img;
ROI(multichannel_mask==0) = 0;

figure;
imshow(ROI);

Upvotes: 0

gregswiss
gregswiss

Reputation: 1456

A bit of a hack but you could create a 'hidden' figure. The only difference is that I added: figure('Visible', 'off') at the start of your code.

figure('Visible', 'off');

img=imread('peppers.png');
h_im = imshow(img); %I want to get rid of imshow because I don't want to show the image

[height, width, planes]=size(img);
%(cX,cY) is image center
cX=width/2;
cY=(height)/2;

%Here I define my ROI which is an ellipse that stretches to 75 percent of
%height and width of the image
e=imellipse(gca,[(1/2-3/8)*width, (1/2-3/8)*height,(3/4)*width,(3/4)*height]);
mymask=createMask(e,h_im);

%extending mask to three channels
mymask=repmat(mymask,[1 1 3]);
ROI=img;
ROI(mymask==0)=0;
figure, imshow(ROI);

Upvotes: 0

Related Questions