How to identify black dots using Matlab
15 次查看(过去 30 天)
显示 更早的评论
Dear all,
Recently I did an experiment and I want to know the position of the tube as a function of time. So I drew some dots on the tube to know the variety. Unfortunately I cannot identify the dots now, maybe they are not obvious. Someone can help me identify black dots?
Here is one of the original image,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/162445/image.jpeg)
And now is my codes:
Img=imread('original image.jpg');
Gimg=rgb2gray(Img);
figure(1);imshow(Gimg);
figure(2);imshow(Img);
%grey point less than 95
BW=Gimg<95;
%Find Weighted Centroid of the chosen dots and plot them together with original image
rp=regionprops(BW,Gimg,'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(Img); 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);
The out put is like this pic.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/162446/image.jpeg)
0 个评论
回答(2 个)
Image Analyst
2017-4-4
编辑:Image Analyst
2017-4-4
Simple global thresholding is not going to be good. What you need to do is to use a bottom hat filter, imbothat() (or imtophat if that doesn't work). Then do some shape filtering to get rid of blobs with a circularity not close to a circle.
Even better is to fix the image capture situation. Why not make the whole tube black? Or some contrasting color, like bright red? I think that's the best fix. Or (less good), how about if you put a string of LED lights along the tube? How about you put reflective dots on it and shine light in from the front? It's far better to capture a good image right from the start rather than try to do post processing to fix up a bad image.
0 个评论
Carl
2017-4-3
编辑:Carl
2017-4-3
Hi Rui, first, here are some general resources on doing Object Detection in MATLAB:
https://www.mathworks.com/discovery/object-detection.html
From looking at your code, it looks like you have some pretty complex logic to essentially remove centroids (set them to 0,0) based on their position. Another idea to consider would be to "remove" centroids based on the size of the regions instead. That is, if the area it represents is too large or too small, because then it wouldn't correspond to the black dots.
You can use the following call to 'regionprops':
rp=regionprops(BW,Gimg,{'WeightedCentroid', 'Area'});
And replace your loop with something like the following:
for j=1:n
Position(j,1)=rp(j).WeightedCentroid(1);
Position(j,2)=rp(j).WeightedCentroid(2);
Area(j) = rp(j).Area;
end
for i=1:n
if Area(i) > 30 || Area(i) < 10
Position(i,1) = 0; Position(i,2) = 0;
end
end
When I tried this out on my machine, it gave a much more accurate representation of where the black dots were located.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!