slicing an M-by-N-by-3 matrix using an M-by-N matrix of logical values

7 ビュー (過去 30 日間)
Luca Amerio
Luca Amerio 2018 年 4 月 11 日
コメント済み: Walter Roberson 2018 年 4 月 12 日
Hi everybody
I have an RGB image as a MxNx3 matrix.
I would like to slice it using an MxN matrix of logic values (one for each pixel) and add a 1x1x3 value to those pixels. I'm trying to a clean way to do it.
In other words, I'm trying to do something like this
A = rand(200,200,3);
i = rand(200,200) > 0.5;
C = cat(3,0.3,0.4,0.2);
A(i) = 0.5*A(i,:) + 0.5*C; %doesn't work!
The only ways I found is either "unpacking" each MxN submatrix to a new variable like this
R = A(:,:,1);
G = A(:,:,2);
B = A(:,:,3);
R(i) = 0.5*R(i) + 0.5*C(1);
G(i) = 0.5*G(i) + 0.5*C(2);
B(i) = 0.5*B(i) + 0.5*C(3);
Anyone got a better idea?

採用された回答

Walter Roberson
Walter Roberson 2018 年 4 月 12 日
A = A + repmat(i, [1, 1, 3]) .* repmat(C, [size(A,1), size(A,2), 1]);
Since R2016b this can be expressed as
A = A + i .* C;
  2 件のコメント
Luca Amerio
Luca Amerio 2018 年 4 月 12 日
The first one is nice, but the second one is great!
Nice one, thank you!
Walter Roberson
Walter Roberson 2018 年 4 月 12 日
dpb's Answer takes into account the selective 1/2 where mine does not. The fix would be
A = A + (C - A)/2 *.i;

サインインしてコメントする。

その他の回答 (1 件)

dpb
dpb 2018 年 4 月 12 日
A=0.5*(A.*i+C.*i);
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 4 月 12 日
Note that this requires R2016b or later due to differences in the sizes of the objects.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by