How do I find the boundaries of a value in a matrix?

Hi, I have a matrix as follows:
I =
1 1 1 1 8 1 2
1 1 8 8 8 2 1
1 8 8 1 2 1 1
1 1 8 2 1 1 1
2 2 2 1 1 1 1
2 2 2 1 1 1 1
I want a matrix which will be output of boundary value of 8 i.e. (1,5),(2,3),(2,4),(2.5),(3,2),(3.3),(4.3)
Is there any way to he find these location easily?

 採用された回答

Sean de Wolski
Sean de Wolski 2011 年 6 月 3 日

0 投票

[r c] = find(bwperim(I==8));

3 件のコメント

Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 5 日
sorry, this also not giving me proper output.
Walter Roberson
Walter Roberson 2011 年 6 月 5 日
That's because you keep changing the stated requirements :(
Ivan van der Kroon
Ivan van der Kroon 2011 年 6 月 6 日
This method and Andrei's still gives the (1,4) and leaves the (3,3).

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

その他の回答 (4 件)

Walter Roberson
Walter Roberson 2011 年 5 月 31 日

3 投票

If you have the image processing toolbox, you can use
bwboundary(I==8)

3 件のコメント

Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 1 日
your code gives me the following error:
??? Undefined function or method 'bwboundary' for input arguments of type 'logical'.
I don't know why? would you please explain me,what should I do?
Thanks
Walter Roberson
Walter Roberson 2011 年 6 月 1 日
http://www.mathworks.com/help/toolbox/images/ref/bwboundaries.html
It appears you do not have the Image Processing Toolbox installed, or else you have a very old version. The Image Processing Toolbox is extra cost for all MATLAB editions except for the Student Edition.
Sean de Wolski
Sean de Wolski 2011 年 6 月 2 日
Typo Walter: bwboundaries(I==8)

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

Ivan van der Kroon
Ivan van der Kroon 2011 年 5 月 31 日

2 投票

[rows,cols]=find(I==8)
rows =
3
2
3
4
2
1
2
cols =
2
3
3
3
4
5
5
Hope this helps.

7 件のコメント

Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 5 月 31 日
If my matrix becomes:
I =
1 1 1 1 8 1 2
1 1 8 8 8 2 1
1 8 8 8 8 1 1
1 1 8 8 8 1 1
2 2 2 1 1 1 1
2 2 2 1 1 1 1
In this case your code will not give proper output. It will give all location of 8
Matt Fig
Matt Fig 2011 年 6 月 1 日
What would be your expected result in this case?
Walter Roberson
Walter Roberson 2011 年 6 月 1 日
Mohammad's expected result would exclude any of the 8's that are eight-connected only to other 8's: he wants only the 8's that are boundary pixels.
Matt Fig
Matt Fig 2011 年 6 月 3 日
I was wondering if Mohammad wanted 4 or 8 connected 8s excluded... I.e., in his example above, are there two 8s excluded or 1?
Walter Roberson
Walter Roberson 2011 年 6 月 3 日
Hmmm, hard to say for sure, as his original example did not happen to include any 8's that were 4 connected to other 8's.
Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 5 日
I =
1 1 8 8 8 1 2
1 1 8 8 8 2 1
1 8 8 8 8 1 1
1 1 8 2 1 1 1
2 2 2 1 1 1 1
2 2 2 1 1 1 1
output needs the following:
(1,3) (1,5) (2,3) (2,5)(3,2)(3,3)(3,4)(3,5) (4,3)
perhaps this will help for better understanding my output
Walter Roberson
Walter Roberson 2011 年 6 月 5 日
So to confirm, any "8" that has only 8's as neighbors is to be excluded, and all other 8's are to be included?
If the entire matrix was 2x2 and was
88
88
then everything should be excluded?
and for
881
888
881
then the entire left side is to be excluded?
Just include the 8's for which at least one of the neighbors is a non-8 ?

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

Andrei Bobrov
Andrei Bobrov 2011 年 6 月 2 日

1 投票

more variant
I1 = I == 8;
l1 = [ones(1,size(I1,2)); diff(I1)~=0];
l2 = [ones(size(I1,1),1) diff(I1,1,2)~=0];
lud = [l1(2:end,:);false(1,size(I1,2))];
llr = [l2(:,2:end) false(size(I1,1),1)];
[j i] = find(((lud+l1+llr+l2)&I1)');
out = [i j];
more more variant
I1 = I==8;
I2=ones(size(I1)+2);
I2(2:end-1,2:end-1) = I1;
I1(conv2(I2,ones(3),'valid')==9)=0;
[i j] = find(I1);
without conv2
I1 = I==8;
I2=ones(size(I1)+2);
I2(2:end-1,2:end-1) = I1;
[i j] = find(I1);
ij1 = [i j];
ij2 = ij1 + 1;
IJ = arrayfun(@(x)bsxfun(@plus,ij2(:,x),[-1 0 1]),1:size(ij2,2),'un',0);
ij1(arrayfun(...
@(x)sum(reshape(I2(IJ{1}(x,:),IJ{2}(x,:)),[],1)),1:size(ij2))==9,:) = [];

3 件のコメント

Mohammad Golam Kibria
Mohammad Golam Kibria 2011 年 6 月 5 日
I =
1 1 8 8 8 1 2
1 1 8 8 8 2 1
1 8 8 8 8 1 1
1 1 8 2 1 1 1
2 2 2 1 1 1 1
2 2 2 1 1 1 1
[i j] gives the following output
ans =
3 2
1 3
2 3
4 3
1 4
3 4
1 5
2 5
3 5
But I need the following output:
(1,3) (1,5) (2,3) (2,5)(3,2)(3,3)(3,4)(3,5) (4,3)
Andrei Bobrov
Andrei Bobrov 2011 年 6 月 6 日
small correction in answer
Andrei Bobrov
Andrei Bobrov 2011 年 6 月 6 日
more correction

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

Teja Muppirala
Teja Muppirala 2011 年 6 月 6 日

1 投票

This will give all the 8's that are not entirely surrounded by other 8's. Assuming you have the Image Processing Toolbox.
[i,j] = find( (I==8) - imerode(I==8,ones(3)) )

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by