2

有没有什么好方法可以分配一些具有相同颜色的蒙版像素,除了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`
4

1 回答 1

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(:))

于 2013-07-11T07:48:13.887 回答