matlab中定义掩码的代码

matlab中定义掩码的代码,matlab,image-processing,machine-learning,computer-vision,Matlab,Image Processing,Machine Learning,Computer Vision,我正在开发CBIR系统,在该系统中,我必须以以下方式分割RGB图像: 我正在用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, pl

我正在开发CBIR系统,在该系统中,我必须以以下方式分割RGB图像:

我正在用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);

有点像黑客,但你可以创建一个“隐藏”的图形。唯一的区别是我在代码开头添加了:
图形(“可见”,“关闭”)

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

您可以自己生成椭圆掩码,而不必使用
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]);
%创建与图像大小相同的网格以生成遮罩
[xy]=meshgrid(1:size(img,1),1:size(img,2));
%使用eclipse的一般形式创建eclipse掩码
%将集中在图像的中间。
%其高度和宽度为该年龄的75%
A=(0.75/2)*尺寸(img,2);
B=(0.75/2)*尺寸(img,1);

mask=A^2*(x-floor(size(img,1)/2))。^2+B^2*(y-floor(size(img,2)/2))。^2我认为此代码易于理解,并且易于调整以处理图像的任意部分(其输出与代码相同):

img=imread('peppers.png');
%定义椭圆相对于图像尺寸的大小
系数=0.75;
hwidth=尺寸(img,2)/2.0;
hhheight=尺寸(img,1)/2.0;
a=宽度*系数;
b=高高*系数;
[x,y]=meshgrid(1:hwidth,1:hheight);
%简单的椭圆方程得到你面具的第三部分

右下角\u mask=((x.^2)/a^2+(y.^2)/b^2)我在尝试执行您的代码时出错,您能帮助我吗?错误是???尝试将脚本mask作为函数执行:C:\Users\dell\Documents\MATLAB\mask.m error in==>ellipsemask at 14 masked\u image=img.*repmat(mask,[1,1,3]);抱歉,现在已修复。
C
应该是
mask
。这似乎对我有用,谢谢!你能给我建议一些方法来定义掩码以获得编号为1、2、3和4的区域吗?
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);