2011-10-04 2 views
2

이미이 작업을 수행하는 함수를 구현했지만 루프가 필요하지 않습니다. 그러나 루프를 피할 수있는 방법은 없습니다. 호기심이 더 좋은 방법이 있다면.효율적으로 MATLAB 이미지의 픽셀 목록을 페인트하는 방법은 무엇입니까?

function [ colored_img ] = colorImg (img, ix, c) 
% function [ colored_img ] = colorImg (img, ix, c) 
% 
% INPUTS 
% img - a 3D array representing the image to color. 
% ix - the indexes of the pixels to set to a new color. 
% c - a vector representing the color to paint the pixels ix. 
% 
% OUTPUTS 
% colored_img - the colored image. 


colored_img = img; 
for jx = 1 : numel(c); 
    a = colored_img(:,:,jx); 
    a(ix) = c(jx); 
    colored_img(:,:,jx) = a; 
end 




end 
+0

3 차원이란 무엇입니까? (R, G, B)? –

답변

0

색상 색인을 복제 할 수 있습니다.

imageSize = numel(img(:,:,1)); 
rgbIdx = bsxfun(@plus,ix(:),(0:2)*imageSize); 

%# replicate the color so that there are as many 
%# entries as pixels to recolor 
%# (skip this if you use a colormap 
%# of length nPixelsToRecolor) 
nPixelsToRecolor = length(ix); %# assign this so that comment makes sense 
repColor = repmat(c,nPixelsToRecolor,1); %# assuming color is 1-by-3 

colored_img = img; 
colored_img(rgbIdx(:)) = repColor(:); 
+0

흠, 제 3 차원은 단순히 R, G, B가 아닌 컬러 벡터의 크기가 같다고 코드에서 보입니다. –

+0

@Laurent ': OP가 코드가 그레이 스케일과 RGB 이미지 모두에서 작동하기를 원한다고 가정합니다. 따라서 색상 벡터 위로 반복합니다. – Jonas

+0

와우, 멋지다. 왜 그런 개념적으로 간단한 문제가 구현하기가 어렵습니까? 그 일부 매우 밀도가 matlab에 라인입니다 ... – John

관련 문제