2

MATLAB に 2336x3504 RGB uint8 ファイルがあります。また、インデックス表記 (2336x3504 のバイナリ イメージに基づく) の対象ピクセルで構成されるベクトルもあります。対象のピクセルに対応する 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);

これは機能しますが、面倒なようです。もっとエレガントな解決策があると確信していますが、私にはわかりません。

4

2 に答える 2

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
于 2014-03-07T09:52:24.927 に答える
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;

このアプローチの利点は、チャネルの新しいマトリックスを作成しないことです。

読み物:マトリックス索引付け.

于 2012-12-03T19:44:56.617 に答える