how to detect empty rows and columns from 3-D matrix and crop them?

조회 수: 13 (최근 30일)
I have a 3-D matrix as a video. It contains empty (0) zero values along the tree dimentions.
The matrix is represented as a video. So I need to:
  • if the row along the frames is empty (==0), I want to crop it.
  • if the column along the frames is empty (==0), I want to crop it
I have an example 3-D dimention but I need a general method.
I have tried to write something but there is something wrong.
Please Help.
%% define the video (3-D matrix)
Frames = [0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0;0 0 0 0 0];
Frames(:,:,2 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,3 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,4 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,5 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,6 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,7 ) = [0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0;0 0 0 0 0];
Frames(:,:,8 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,9 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,10) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
%% define some varibles
%% n & m are zeros counters
%% x,y,z are lengths along the three dimentions
n=0;
m=0;
x=length(Frames(:,1,1));
y=length(Frames(1,:,1));
z=length(Frames(1,1,:));
%% if the row along the frames is empty (==0),crop it.
%% r for row
% %% c for columns
% %% f for frames
for c=1:y
for r=1:x
for f=1:z
if Frames(r,c,f)==0 %detect
m=m+1;
end
end
if m==25
Frames(:,c,:) = []; %crop
end
end
end
%% %% if the column along the frames is empty (==0),crop it.
%% r for row
% %% c for columns
% %% f for frames
for r=1:x
for c=1:y
for f=1:z
if Frames(r,c,f)==0 %detect
n=n+1;
end
end
if m==25
Frames(r,:,:) = []; %crop
end
end
end

채택된 답변

Matt J
Matt J 2018년 12월 31일
편집: Matt J 2018년 12월 31일
tmp=any(Frames,3);
I = any(tmp,2);
J= any(tmp,1);
Frames=Frames(I,J,:);
  댓글 수: 3

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Stephan
Stephan 2018년 12월 31일
편집: Stephan 2018년 12월 31일
Hi,
two lines of code do the job:
Frames = zeros(5,5,10);
Frames(:,:,1 ) = [0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0;0 0 0 0 0];
Frames(:,:,2 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,3 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,4 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,5 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,6 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,7 ) = [0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0;0 0 0 0 0];
Frames(:,:,8 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,9 ) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
Frames(:,:,10) = [0 0 0 0 0;0 0 90 0 0; 0 0 90 0 0; 90 90 90 0 0;0 0 0 0 0];
% crop all empty rows and columns
Frames(:,find(sum(Frames,[1 3])==0),:)=[];
Frames(find(sum(Frames,[2 3])==0),:,:)=[]
disp('Please accept useful answers.')
Best regards
Stephan
  댓글 수: 2
Nibras Abo Alzahab
Nibras Abo Alzahab 2018년 12월 31일
Thank you for your answer.
I got an error while using the
Error using sum
Dimension argument must be a positive integer scalar within indexing range.
Frames(:,find(sum(Frames,[1 3])==0),:)=[];
Stephan
Stephan 2018년 12월 31일
편집: Stephan 2018년 12월 31일
Hm, works for me. Which Release do you use? however - shortest answer from Matt is a nice one.

댓글을 달려면 로그인하십시오.


Luna
Luna 2018년 12월 31일
편집: Luna 2018년 12월 31일
Hi,
Try this below:
%% delete zero rows
Frames = reshape(Frames(bsxfun(@times,any(Frames,2),ones(1,size(Frames,2)))>0),[],size(Frames,2),size(Frames,3));
%% delete zero columns
Frames = reshape(Frames(bsxfun(@times,any(Frames,1),ones(size(Frames,1),1))>0),size(Frames,1),[],size(Frames,3));
you will get 3x3x10 matrix
  댓글 수: 6
Stephan
Stephan 2018년 12월 31일
편집: Stephan 2018년 12월 31일
The questioner will let us know...
Nibras Abo Alzahab
Nibras Abo Alzahab 2018년 12월 31일
Thank you for your intrest and ansewrs.
Luna is right
I souhld get 4x3x10 matrix.
But in your answer: 3x3x10 matrix

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by