将RGB图像的某种颜色的像素更改为另一种颜色Matlab

Change pixels of certain color of an RGB image into another color Matlab

我有一个 RGB 图像 20x100x3,我想将具有颜色 17,167,243 的像素更改为这种颜色 108,5,15。如果有人可以请教如何在 Matlab 中完成此操作?

不是最优雅的解决方案:找到矩阵 image(:,:, 1) 的索引等于 17(使用 find()),然后 image( :,:, 2) 等于 167,然后 (:, :, 3)...然后识别所有三个列表中的所有索引(使用 ismember())。将 (:, :, x) 矩阵中这些像素的值更改为各自请求的 RGB 值。

假设 img 作为输入图像数组,这可能是 bsxfun -

的一种方法
oldval = [17,167,243]
newval = [108,5,15]

idx = find(all(bsxfun(@eq,img,permute(oldval,[1 3 2])),3))
idx_all = bsxfun(@plus,idx(:),[0:2]*numel(img(:,:,1)))
img(idx_all) = repmat(newval,numel(idx),1)

或者使用 logical indexing 而不是之前使用的基于 linear indexing 的方法稍作修改 -

mask = all(bsxfun(@eq,img,permute(oldval,[1 3 2])),3)
img(repmat(mask,1,1,3)) = repmat(newval,sum(mask(:)),1)