필터 지우기
필터 지우기

How can I accurately plot the centroid of black dots in MATLAB?

조회 수: 44 (최근 30일)
PamunkeBoy
PamunkeBoy 2018년 6월 6일
댓글: PamunkeBoy 2018년 6월 18일
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,clc
grid on
X = imread('redbox_5.bmp');
figure
imagesc(X)
se = strel('disk',3);
J = imsubtract(imadd(X,imtophat(X,se)),imbothat(X,se));
figure
imshow(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);

채택된 답변

Anton Semechko
Anton Semechko 2018년 6월 7일
% Get the image
im=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/120388/MATLAB_QUESTION.PNG');
im=max(im,[],3);
% Segment blobs
bw=imclose(im<60,strel('disk',5));
% Cenroids of individual blobs
RP=regionprops(bw,'Centroid');
N=numel(RP);
C=zeros(N,2);
for i=1:N, C(i,:)=RP(i).Centroid; end
% Centroid of all blobs
C_net=regionprops(double(bw),'Centroid');
C_net=C_net.Centroid;
figure
imshow(bw)
hold on
plot(C(:,1),C(:,2),'or','MarkerSize',10,'LineWidth',2)
plot(C_net(:,1),C_net(:,2),'*g','MarkerSize',10,'LineWidth',2)
  댓글 수: 5
Image Analyst
Image Analyst 2018년 6월 16일
It
(1) removes blobs that are not in the range (2 numbers) you specify, or
(2) removes or keeps the N largest or smallest blobs if you pass in a single number.
PamunkeBoy
PamunkeBoy 2018년 6월 18일
Okay. I was also having trouble coding with imclose(). How am I supposed to code this?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by