用于在matlab中定义掩码的代码

时间:2015-10-28 04:14:05

标签: matlab image-processing machine-learning computer-vision

我正在开发一个CBIR系统,我必须按以下方式分割我的RGB图像:

Regions I want to divide my Image into

我在matlab中实现代码,但我无法为它构建适当的掩码。 我使用imellipse,但这需要使用imshow实现的图像处理,但我不想显示我的图像。 我的代码是

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

3 个答案:

答案 0 :(得分:2)

您可以自己生成椭圆蒙版,而不是使用imellipse命令。

% 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]);

答案 1 :(得分:0)

有点黑客但你可以创建一个隐藏的&#39;数字。唯一的区别是我在代码的开头添加了figure('Visible', 'off')

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);

答案 2 :(得分:0)

我认为这段代码易于理解,并且可以轻松调整以处理图像的任意部分(它与您的代码具有相同的输出):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
    A text outside any tag<br/>
    <input type="button" id="iii" value="xxx"/>
    Another text<br/>
    <a href="#">A link</a>
</div>