How to identify black dots using Matlab

25 ビュー (過去 30 日間)
Rui Zhu
Rui Zhu 2017 年 3 月 31 日
編集済み: Image Analyst 2017 年 4 月 4 日
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,
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.

回答 (2 件)

Image Analyst
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.

Carl
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.
  1 件のコメント
Rui Zhu
Rui Zhu 2017 年 4 月 4 日
Yes indeed, I tried your code. The 'area' is way much better than the logic I used. Thank you for the help.
On the other hand, the detect method I've also check. I don't know which one is proper. Did you try one of them from the link or you just use my old codes.
I used my old skill to check the dots identify together with your advice and pic comes out
still miss some points especially the dark part close to the interface.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by