2013-07-11 76 views
2

除了for-loop之外,有沒有什麼好的方法去分配一些相同顏色的蒙版像素?Pixel-wise like assignment在Matlab中

%% 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` 
+0

你可以重塑你的形象,應用面具,並重塑形狀......如果這就是你的意思 – bla

+0

@natan我已經使用'repmat()'添加了我自己的解決方案,但它有點尷尬。如果你使用'reshape()'更簡單的解決方案,我很樂意看到它。 – user1884905

+0

需要'reshape','permute'和'repmat'的組合。看看他們所有的文件。 – rwong

回答

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

+0

是的,這與'deal()'的想法非常相似。感謝您的解決方案:) – Xiaolong