How to identify area of a matrix with same values

3 次查看(过去 30 天)
I have a matrix of 10x10 filled with 0s and 1s. There are some areas of 0s enclosed by areas of 1s. I would like to be able to identify specific areas of 0s.
I am using ginput, so I would like to select a 0 from the matrix, and then I want Matlab to identify the area surrounding that specific 0. I believe this can be done with conv2, but I don't know how.

回答(1 个)

YT
YT 2018-10-19
So if I understand it corect you want something like this
%small 5x5 example matrix
A = [0 1 0 0 0;
1 1 0 0 0;
0 0 1 1 1;
0 0 1 0 1;
0 0 1 1 1];
%create matrix with zeros with size of A
M = zeros(size(A));
%use the coordinates from ginput; M(row,col)
M(1,1) = 1;
%convolution
C = conv2(M,[1,1,1;1,0,1;1,1,1],'same')>0;
%return indices
[row col] = find(C>0);
%return values (which in your case isnt really interesting cuz will only returns 1's)
A(C)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by