How to clean a Set of points based on the nearby points

3 vues (au cours des 30 derniers jours)
Kasra Sadatsharifi
Kasra Sadatsharifi le 29 Jan 2021
Modifié(e) : Matt J le 31 Jan 2021
Hey everybody,
I have a set of points which some are on the grid intersections and some are not. I want to remove the ones that are not on the intersections and take the average of the ones that are. An example showing a part of teh points are attached. The points being one the darker area or the brighter area does not matter. If you look at the points there are sometimes several points on an intersection and sometimes only one. What I had on mind to do with these points was to search the small (5 pixels in euclidean distance) nearby area of each point and see if there is any other point nad take the average of the coordinates which will result in a wanted intersection point. If not, then it is probably a wrongly detected intersection. The idea is clear but I want to know how to do it fast(with the least for loops as possible). If you have a better idea I would be happy to read about it. I have my code below as well. It's not working properly though. grid size is the size of white lines and tag size is the size of the black squares. because of the surface variation of the object the sizes are changed.
function new_points = clean_points(points,opt)
%tagsize = 20;
%gridsize = 4
points_close = [];
points_far = [];
[ind_far] = rangesearch(points,points,opt.tagsize*1.5);
for i = 1:length(ind_far)
far_points_ind = ind_far{i};
far_points = points(far_points_ind,:);
points_far = [points_far;far_points];
end
[ind_close] = rangesearch(points,points,opt.gridsize/sqrt(2));
for i = 1:length(ind_close)
close_points_ind = ind_close{i};
close_points = points(close_points_ind,:);
points_close = [points_close;close_points];
end
clean_points = intersect(points_far,points_close,'rows');

Réponses (1)

Matt J
Matt J le 29 Jan 2021
Modifié(e) : Matt J le 29 Jan 2021
I think you should probably go back to the original image. Once you have a binarization BW of the original image, you can get the coordinates of the grid intersections by doing,
[I,J] = find( bwmorph( bwskel(BW) , 'branchpoints') )
  4 commentaires
Kasra Sadatsharifi
Kasra Sadatsharifi le 29 Jan 2021
By fitting lines to the grayscale and finding the intersection of those lines.
Matt J
Matt J le 30 Jan 2021
Modifié(e) : Matt J le 31 Jan 2021
Well, you can consolidate the points near the intersections using uniquetol as in the code below. However, without a binarized image, I don't know how you would decide which points are too remote from an intersection and should be discarded. That seems to be something you would have to do morphologically. I'm not sure what is undesirable to you about binarizing the image. I was able to do it below quite readily with imbinarize(___,'adaptive').
load(websave('image.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/504458/image.mat'))
B=imbinarize(C,'adaptive');
B=bwareafilt(B,[1000,inf]);
S=bwskel(B);
bp=bwmorph(S,'branchpoints');
S=bwareafilt(S&~bp,[10,inf])|bp;
S=bwskel(imclose(S,strel('disk',15)));
%reg=regionprops(bwmorph( S , 'branchpoints'),'Centroid');
%p0=vertcat(reg.Centroid);
[I,J]=find(bwmorph( S , 'branchpoints'));
p0=[J,I];
[~,~,G]=uniquetol(p0,14,'ByRows',1,'DataScale',1);
p=splitapply(@(z)mean(z,1),p0,G);
imshow(C)
hold on; scatter(p(:,1),p(:,2),60,'r','o','filled'); hold off

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by