Find points covered by connected components

3 views (last 30 days)
em
em on 8 Jan 2016
Commented: Image Analyst on 12 Jan 2016
I have an array A containing a list of (x,y) positions in an image, and a labeled matrix B for the image. How can I find an array C, which stores the index of points in A that is covered by the connected components in B. An example is
A=[1 2
1 5
5 2
5 5];
B= [0 0 0 1 1
0 0 0 1 1
0 0 0 0 0
0 2 2 0 0
0 2 2 0 0];
C= [2
3];
Here is how it looks on images. The first image shows points in A plotted on an image. The second image shows my connected objects. How can I find indexes of points in A that is covered by the connected objects?

Answers (1)

Image Analyst
Image Analyst on 8 Jan 2016
A simple and fast for loop will do it:
A=[1 2
1 5
5 2
5 5];
B= [0 0 0 1 1
0 0 0 1 1
0 0 0 0 0
0 2 2 0 0
0 2 2 0 0];
C= [2
3];
numRows = size(A, 1);
% Create a logical vector that will say whether
% or not the row of A is in B.
rowsToKeep = false(numRows, 1);
for k = 1 : numRows
if B(A(k, 1), A(k, 2)) ~= 0
% It's in there, set it true.
rowsToKeep(k) = true;
end
end
% Turn logical vector into actual indexes.
C = find(rowsToKeep)
  3 Comments
Image Analyst
Image Analyst on 12 Jan 2016
No, it will not be "very slow" - that is a myth. There is a difference between slow er and very slow. Try it and you'll see it's fast enough. Faster than a blink of an eye. Even if your A is hundreds of thousands of rows.
Image Analyst
Image Analyst on 12 Jan 2016
Alright, what is A and B? B is a labeled image, which meant you had to have had a binary image beforehand. So what is A? A has only some of the centroids that are in B. Why? Why does A not have all of the centroids in B? And where did it get the extra points that are not in B at all?
Another option is to use morphological reconstruction. If you have two binary images, marker and mask, then imreconstruct(marker, mask) will return those blobs in mask that fully or partially contain blobs in marker. The thing is though, that to get marker from a list of (x,y) coordinates like you have for A, you'd still have to use a for loop to assign locations in A to the marker binary image.
Perhaps if you told me more of the story of these pictures, like how you got A and B and what you really want to obtain. Perhaps we're solving the wrong problem here, like you think you want to do this but if you told me the complete picture, I'd be able to suggest a much better approach.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!