首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在matlab中定义掩码的代码

在matlab中定义掩码的代码
EN

Stack Overflow用户
提问于 2015-10-28 04:14:04
回答 3查看 376关注 0票数 1

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

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

代码语言:javascript
复制
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);

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-10-28 05:05:36

有点黑客,但你可以创建一个“隐藏”的数字。唯一的区别是,我在代码开始时添加了:figure('Visible', 'off')

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2015-10-28 05:45:23

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

代码语言:javascript
复制
% 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]);
票数 2
EN

Stack Overflow用户

发布于 2015-10-28 19:16:50

我认为这段代码很容易理解,并且很容易调整以解决图像中任意部分的问题(它的输出与代码相同):

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33382802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档