在Matlab中像Pixel一样分配

时间:2013-07-11 06:25:03

标签: image matlab image-processing

有没有什么好方法可以分配一些除for-loop以外的相同颜色的蒙版像素?

%% pick a color
cl = uisetcolor; %1-by-3 vector

im = ones(3, 3, 3)  / 2; % gray image

mask = rand(3, 3); 
mask_idx = mask > 0.5; % create a mask

%% Something like this
im(mask_idx, :) = cl'; % assignment the pixels to color `cl`

1 个答案:

答案 0 :(得分:1)

你可以这样做,使用repmat()

%% pick a color
cl = uisetcolor; %1-by-3 vector
im = ones(3, 3, 3)/2; % gray image
mask = rand(3, 3);
mask_idx = mask > 0.5; % create a mask

cl_rep = repmat(cl,[sum(mask_idx(:)) 1]);
im(repmat(mask_idx,[1 1 3])) = cl_rep(:);

我所做的是重复三次遮罩以获得图像的所有三层。为了能够将其与颜色矢量cl相匹配,还必须重复它。重复的次数与要更改的像素数sum(mask_idx(:))相同。

相关问题