Transform 3D binary matrix into 2D
2 ビュー (過去 30 日間)
古いコメントを表示
Dear colleagues,
I would like to kindly ask you for the help with the transformation from 3D into 2D matrix. Supposing that I have stored the binary image in the variable seg1, containing 45 layers (seg1(:,:,45)). I need to make the 2D matrix with the same dimension(rows and columns) as seg1 with keeping the same pixel values: original=1, result=1 and original=0, result=0. Here is my code, but it does not work well. Thank you very much for your help.
[row col lev]=size(seg1);
seg2=zeros(row,col);
for i=1:lev
for j=1:row
for k=1:col
if seg1(j,k,i)=1
seg2(j,k)=1;
else
seg2(j,k)=0;
end
end
end
end
0 件のコメント
回答 (1 件)
Jan
2021 年 4 月 13 日
Your code overwrites the output for each loop over i=1:lev. An equivalent code without a loop:
%seg2 = seg1(:, :, end);
What should happen with the 45 bits of the input? Do you want to store them as bits of integer values?
seg1 = randi([0,1], 3,4,5);
[row, col, lev] = size(seg1);
tmp = reshape(seg1, row*col, lev) * pow2(lev-1:-1:0).';
seg2 = reshape(tmp, row, col);
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Read, Write, and Modify Image についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!