Check if nxm block of 1s is inside binary matrix

3 次查看(过去 30 天)
I have a 90x89 binary matrix (see attached). In short, the 1s are good and 0s are bad. I want a quick way to check and return any nxm blocks of all 1s within the large matrix. For example, I want to find out if there are any 3x6 (n=3, m=6) blocks of all 1s within the binary matrix. If there are, return the row and col for each cell within each block.

采纳的回答

Bruno Luong
Bruno Luong 2018-10-5
编辑:Bruno Luong 2018-10-5
[i,j] = find(conv2(MM_bin,ones(3,6),'same')==3*6);
I was testing with MM_bin in your MATFILE file which is an 2D array (a matrix in short) that contains 0/1.
If you test on other array with more dimensions than 2, then it's normal that you get error.
  4 个评论
Andrew Poissant
Andrew Poissant 2018-10-5
I am looking at it but the sub block of 1s is 3x6, so the "center" provided by the convolution isn't actually a centroid because it has an even number of points in one direction.
Bruno Luong
Bruno Luong 2018-10-5
编辑:Bruno Luong 2018-10-5
Yes the return index is shifted by half index for even size.
To compensate, dimension indexes of the box is (so single (i,j) pair)
wi = 3;
di = (wi-1)/2;
ibox = ceil(i-di) : ceil(i+di);
wj = 6;
dj = (wj-1)/2;
jbox = ceil(j-dj) : ceil(j+dj);
% MM_bin(ibox,jbox) == ones(3,6);

请先登录,再进行评论。

更多回答(1 个)

Bruno Luong
Bruno Luong 2018-10-5
编辑:Bruno Luong 2018-10-5
[i,j] = find(conv2(MM_bin,ones(3,6),'same')==3*6);
will return you all the positions (i,j) with 3*6 submatrix of ones centered at this position.
  1 个评论
Andrew Poissant
Andrew Poissant 2018-10-5
Thank you for your answer. I get the following error:
Error using conv2
N-D arrays are not supported.
Error in FM5_LowBattery_newLZ (line 197)
[ii,jj] = find(conv2(MM_bin_land_windowed,ones(3,6),'same')==3*6);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by