I have an Image in which I am trying to determine the centroid of the black dots. My Code seems to have trouble recognizing the centroid for all black dots as their respective shade of grey differs. Would anyone be able to guide me into how I could diagnose this? Thank you!
%%Centroid
clear,clcgrid onX = imread('redbox_5.bmp');figureimagesc(X)se = strel('disk',3);J = imsubtract(imadd(X,imtophat(X,se)),imbothat(X,se));figureimshow(J)newmap2 = contrast(J,2);colormap(newmap2)figure(2);imshow(J);%grey point less than 30
BW=J<30;%Find Weighted Centroid of the chosen dots and plot them together with original image
%https://www.mathworks.com/matlabcentral/answers/332938-how-to-identify-black-dots-using-matlab
rp=regionprops(BW,J,'WeightedCentroid'); n=numel(rp); for j=1:n Position(j,1)=rp(j).WeightedCentroid(1); Position(j,2)=rp(j).WeightedCentroid(2); end for i=1:n-1 if (Position(i+1,1)-Position(i,1))<5 || (Position(i+1,2)-Position(i,2))<5 Position(i+1,1)=1/2*(Position(i,1)+Position(i+1,1)); Position(i+1,2)=1/2*(Position(i,2)+Position(i+1,2)); Position(i,1)=0;Position(i,2)=0; end end [m,n]=size(Position); figure(3);imshow(J); axis image; hold on; for i=1:m for j=1:n-1 plot(Position(i,j),Position(i,j+1),'r*') end end Position(all(Position==0,2),:)=[]; display(Position);
Best Answer