Obtain subscript values of common diagonal rectangle in binary matrix?

2 ビュー (過去 30 日間)
Andrew Poissant
Andrew Poissant 2018 年 10 月 12 日
コメント済み: Image Analyst 2018 年 10 月 12 日
I am trying to find where in the MM_bin binary matrix (see attached MM_bin.mat file) contains the check_mat matrix (see code below), and then extract the subscript coordinates in the MM_bin matrix of the 1s from the check_mat matrix. So far I am able to use a 2D convolution to extract the centroids of the spots where the check_mat matrix is found in the MM_bin matrix, but I am unsure how to extract the subscript coordinates in the MM_bin matrix for each centroid.
load('MM_bin.mat')
check_mat = [0 0 0 0 0 1 1 1 1;
0 0 0 0 1 1 1 1 0;
0 0 0 1 1 1 1 0 0;
0 0 1 1 1 1 0 0 0;
0 1 1 1 1 0 0 0 0;
1 1 1 1 0 0 0 0 0];
minLi = 6; % length of the 1s diagonal
minLj = 4; % width of the 1s diagonal
[i_cent, j_cent] = find(conv2(MM_bin(:,:,1), check_mat, 'same') == minLi*minLj)
  5 件のコメント
Andrew Poissant
Andrew Poissant 2018 年 10 月 12 日
Hm, maybe I am using it incorrectly. But I want to find the pattern of 1s seen in the check_mat matrix. The 0s are simply placeholders/are there to make the desired diagonal pattern of 1s. I just want to find where the pattern of 1s seen in the check_mat matrix is found in the MM_bin matrix. Maybe I should try to ask the question a different way
Guillaume
Guillaume 2018 年 10 月 12 日
If you're just looking for the pattern of ones and you don't care if the 0s in your check_mat match a 0 or a 1, then your code is correct and I misunderstood. If the 0s must match a 0, then it cannot work with a convolution.
Assuming your code is correct, I still don't understand what final out you want. Perhaps, provide an example with smaller matrices.
E.G., with
MM_bin = [0 1 0 1 0
0 1 1 1 1
1 0 1 1 1]
check_mat = [0 1
1 0]
what final output do you want?

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

採用された回答

Bruno Luong
Bruno Luong 2018 年 10 月 12 日
編集済み: Bruno Luong 2018 年 10 月 12 日
% Fake data
MM_bin=rand(100)>0.2;
check_mat = [0 0 0 0 0 1 1 1 1;
0 0 0 0 1 1 1 1 0;
0 0 0 1 1 1 1 0 0;
0 0 1 1 1 1 0 0 0;
0 1 1 1 1 0 0 0 0;
1 1 1 1 0 0 0 0 0];
s = size(check_mat);
% Careful if your pattern is not symmetric you must flip it
% for each dimension when using with CONV
[i_cent, j_cent] = find(conv2(MM_bin(:,:,1), ...
fliplr(flipud(check_mat)), 'same') == sum(check_mat(:)));
[ip,jp] = find(check_mat);
i = floor(i_cent(:)-s(1)/2)+ip(:).';
j = floor(j_cent(:)-s(2)/2)+jp(:).';
% remove redundancy
ij = unique([i(:) j(:)],'rows');
% graphical check
close all
imagesc(MM_bin);
hold on
plot(ij(:,2),ij(:,1),'.r') % NOTE: x-axis is second dimension
axis equal
  1 件のコメント
Andrew Poissant
Andrew Poissant 2018 年 10 月 12 日
Beautiful! This was very helpful thank you! So I was on the right track but needed some tweaks.

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

その他の回答 (3 件)

Image Analyst
Image Analyst 2018 年 10 月 12 日
To find the centroid of the 1's, if you have the Image Processing Toolbox, you can use regionprops
check_mat = [0 0 0 0 0 1 1 1 1;
0 0 0 0 1 1 1 1 0;
0 0 0 1 1 1 1 0 0;
0 0 1 1 1 1 0 0 0;
0 1 1 1 1 0 0 0 0;
1 1 1 1 0 0 0 0 0];
props = regionprops(check_mat, 'Centroid');
xCentroid = props.Centroid(1)
yCentroid = props.Centroid(2)
  9 件のコメント
Bruno Luong
Bruno Luong 2018 年 10 月 12 日
編集済み: Bruno Luong 2018 年 10 月 12 日
Or simply change the single conv2 check to
conv2(Im,p,'same')==sum(p(:)) & conv2(Im,1-p,'same')==sum(1-p(:))
since they are both binary images.
Image Analyst
Image Analyst 2018 年 10 月 12 日
It's not clear to me what "(minus the 0s)" means. Maybe "ignoring any zeros"??? In other words, as long as that parallelogram is there, it doesn't matter if, outside the parallelogram, it's all ones, all zeros, or some random pattern or ones and zeros.

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


Image Analyst
Image Analyst 2018 年 10 月 12 日
If you want to find where check_mat occurs in a much larger matrix, simple scan and use isequal().

Matt J
Matt J 2018 年 10 月 12 日
編集済み: Matt J 2018 年 10 月 12 日
[I0,J0]=find(check_mat);
I = I0 + (i_cent.'-3); %I subscripts
J = J0 + (j_cent.'-5); %J subscripts

Community Treasure Hunt

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

Start Hunting!

Translated by