How to find x,y matrix using for sintax

조회 수: 2 (최근 30일)
rio
rio 2013년 6월 20일
i have been working for my thesis about image segmentation. i have this matrix
a =
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 1 0 0
0 0 0 0 1 1 1 1 1 0
0 0 0 0 0 1 1 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
and then i want to find the first x,y with binary 1 using for sintax this is the one i use but its not working
---------------------------------------------
function [x1,y1,x2,y2,x3,y3,x4,y4]=gedge(x)
[m,n]=size(x);
% upper edge xy
for i=1:m
for j=1:n
if(x(i,j)==1)
x1=i;
y1=j;
break
end
end
end
% left edge xy
for j=1:n
for i=1:m
if(x(i,j)==1)
x2=i;
y2=j;
break
end
end
end
% bottom edge xy
for i=m:-1:1
for j=n:-1:1
if(x(i,j)==1)
x3=i;
y3=j;
break
end
end
end
% right edge xy
for j=n:-1:1
for i=m:-1:1
if(x(i,j)==1)
x4=i;
y4=j;
break
end
end
end
%for the cropping
y=x(x1:x4,y2:y3);
-----------------------------------------------------------------
so i want to erase the 0 binary for my image segmentation. it will be this matrix:
a =
0 1 1 1 0
1 1 1 1 1
0 1 1 0 0
0 1 0 0 0
please help recorrect my sintax code. Thanks

채택된 답변

random09983492
random09983492 2013년 6월 20일
Hi,
Here is my implementation. Let me know if there is something you do not understand.
[r,c] = size(a);
% Starts at bottom row and removes rows that have nothing in them.
for i = fliplr(1:r)
if sum(a(i,:)) == 0
a(i,:) = [];
else
break;
end
end
% Starts from right-most column and removes columns with nothing in them.
for i = fliplr(1:c)
if sum(a(:,i)) == 0
a(:,i) = [];
else
break;
end
end
% Starts at top row, counts number of empty rows until it gets to a
% non-empty row. These cannot be removed immediately or it will screw up
% the for-loop.
top_count = 0;
for i = 1:r
if sum(a(i,:)) == 0
top_count = top_count + 1;
else
break;
end
end
% Starts at left-most column, counts number of empty columns until it gets
% to a non-empty column. These cannot be removed immediately or it will
% screw up the for-loop.
left_count = 0;
for i = 1:c
if sum(a(:,i)) == 0
left_count = left_count + 1;
else
break;
end
end
% Now left-most empty columns and top empty rows can be safely removed.
if top_count > 0
a(1:top_count,:) = [];
end
if left_count > 0
a(:,1:left_count) = [];
end
  댓글 수: 1
rio
rio 2013년 6월 20일
i really-really appreciate Mr Elliot answer, this will help me to solve my thesis. hope you succes in the future as well. one again thanks man.

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

추가 답변 (2개)

Jonathan Sullivan
Jonathan Sullivan 2013년 6월 20일
편집: Jonathan Sullivan 2013년 6월 20일
It's really simple if you use the functions find and any. See below:
ind1 = find(any(x == 1,2),1);
ind2 = find(any(x == 1,2),1,'last');
ind3 = find(any(x == 1,1),1);
ind4 = find(any(x == 1,1),1,'last');
y = x(ind1:ind2,ind3:ind4);
  댓글 수: 1
rio
rio 2013년 6월 20일
thanks man, i already try your way and its work too. hope you succes in the future as well. very-very help. thank you so much.

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


Andrei Bobrov
Andrei Bobrov 2013년 6월 20일
out = a(any(a,2),any(a));
  댓글 수: 1
rio
rio 2013년 6월 20일
thanks man, i appreciate it. :)

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by