2012-12-03 53 views
2

我在MATLAB中有一个2336x3504 RGB uint8文件。我还有一个由索引符号中的感兴趣像素组成的矢量(基于2336x3504二进制图像)。我希望RGB图像中与感兴趣像素相对应的所有点都被设置为特定颜色。基于MATLAB中的索引位置将RGB图像中的特定位置更改为特定颜色

我首先想到的是要做到以下几点:

% Separate RGB image into three 3 uint8 arrays. 

RGB1 = RGBImage(:,:,1); 

RGB2 = RGBImage(:,:,2); 

RGB3 = RGBImage(:,:,3); 

% Change each layer based on the color I want (say for red, or [255 0 0]) 

RGB1(interestPixels) = 255; 

RGB2(interestPixels) = 0; 

RGB3(interestPixels) = 0; 

% Then put it all back together 

NewRGBImage = cat(3,RGB1,RGB2,RGB3); 

虽然这个工作,它看起来凌乱。我确定有一个更优雅的解决方案,但我没有看到它。

回答

0

我能找到到目前为止,最简单的方法是这样的:

% test init 
mat = randi(3,[2 4 3]);  % a 3-channel 2x4 image 
interest_pix = [1 1;2 2; 1 3]; % some pixel coordinates 

channel_selector = ones(1,size(interest_pix,1)); 
inds_chn1 = sub2ind(size(mat),interest_pix(:,1), interest_pix(:,2),1*channel_selector); 
inds_chn2 = sub2ind(size(mat),interest_pix(:,1), interest_pix(:,2),2*channel_selector); 
inds_chn3 = sub2ind(size(mat),interest_pix(:,1), interest_pix(:,2),3*channel_selector); 

mat(inds_chn1)=255; % you could also use NaN or Inf to make the changes more apparent 
mat(inds_chn2)=0; 
mat(inds_chn3)=0; 

这种方法的优点是它不会创建渠道新矩阵。

阅读材料:matrix indexing

0

上面的代码有错误。这是正确的。试试吧。

function testmat=testmat() 
mat = rand(3,3,3)% a 3-channel 2x4 image 
[v w c]=size(mat) 
interest_pix = [1 1;2 2; 1 3]; % some pixel coordinates 

channel_selector = ones(1,size(interest_pix,1)); 
c1=1*channel_selector; 
d1(:,1)=c1(1,:); 

c2=2*channel_selector; 
d2(:,1)=c2(1,:) 

c3=3*channel_selector; 
d3(:,1)=c3(1,:); 

inds_chn1 = sub2ind(size(mat),interest_pix(:,1), interest_pix(:,2),1*d1); 
inds_chn2 = sub2ind(size(mat),interest_pix(:,1), interest_pix(:,2),1*d2); 
inds_chn3 = sub2ind(size(mat),interest_pix(:,1), interest_pix(:,2),1*d3); 

mat(inds_chn1)=255; % you could also use NaN or Inf to make the changes more apparent 
mat(inds_chn2)=0; 
mat(inds_chn3)=0; 
end 
相关问题