nini
nini

Reputation: 79

Unable to space out markers in MATLAB

I have an image with white spots all over them. Now, I want to put a marker on each spot based on certain coordinates and checks. Now my problem is, i don't want to cluster too many markers on one particular "blob" of white spots that takes up more than a pixel. My work around is to check if my previous marker location and my current are within proximity. However, this causes a lot of independent white blobs to be missed due to close proximity even if it's not necessarily just one blob.

Here is my current code:

a = find(overlap == 1); %overlap is a 1040 by 1392 binary matrix
prev_coord = [1 1];

for i=1:size(a)
    c = mod(a(i), 1040);
    r = floor(a(i)/1040);
    X = [prev_coord; r c];
    if(pdist(X, 'euclidean') > prox)      
       if(img(r, c) > 1)
           gray = insertMarker(gray, [r c], 'x', 'color', 'red', 'size', 15); 
       else
           gray = insertMarker(gray, [r c], 'x', 'color', 'white', 'size', 15);      
       end
    end
    prev_coord = [r c];
end

When prox equals a very small number like 50, my image looks like this: enter image description here

However, when prox is a large number like 120, it looks like this: enter image description here

Any ideas on how to work around this?

Upvotes: 1

Views: 58

Answers (1)

Leander Moesinger
Leander Moesinger

Reputation: 2462

You can do that using morphological operations:

A = round(overlap./max(overlap(:))) % turn original image into binary image.
A = imfill(A,'holes'); % fill holes in blobs
A = bwmorph(A,'shrink',Inf) % reduce each blob to a single point

Then you can place a marker on each pixel where A==1.

Upvotes: 2

Related Questions