您可以使用以下命令提取各个对象的坐标regionprops
这是一个示例实现:
im=rgb2gray(imread('https://i.stack.imgur.com/jic1X.jpg'));
P=regionprops(~im2bw(im), 'All');
Areas=cell2mat({P.Area});
Centroids=cell2mat({P.Centroid}');
仅选择面积大于 10 但小于 100 的点:
Coord=Centroids(Areas< 100 & Areas > 10,:);
监控发现的点:
imshow(im);
hold on
for k=1:length(Coord)
plot(Coord(k,1), Coord(k,2), 'ro');
hold on
end
Result:
![Dots found](https://i.stack.imgur.com/WhHHr.png)
然后,您可以使用以下方法对点进行排序:
Aboves=Coord(Coord(:,2) < 80,:);
Belows=Coord(Coord(:,2) > 80,:);
从这里开始,有很多方法可以解决您的问题,其中一种选择如下:
dots=[81,183;80,217;83,275;83,314];
DetectDots=zeros(length(dots)-1, 1); % Creating a vector of zeros corresponding to the gaps between the elements in 'dots'
for k=1:size(dots,1)-1
if ~isempty(find((Aboves(:,1) > dots(k,2) & Aboves(:,1) < dots(k+1,2)))) %*
DetectDots(k)=1;
elseif ~isempty(find((Belows(:,1) > dots(k,2) & Belows(:,1) < dots(k+1,2))))
DetectDots(k)=-1;
else
DetectDots(k)=0;
end
end
结果是一个向量DetectDots
有价值[1,1,-1]
在这种情况下,表示向量的第一个点之间以及第二个和第三个点之间有上面的点,以及向量的第三个和最后一个点之间有下面的点dots
.
*find
returns a logical array with ones where the condition is met. isempty
checks if the output of find
has at least one element. As a result the condition is one if there is at least one element in the array Aboves
or Belows
that meets the criteria. ~
is the logical NOT, hence ~=
means not equal. &
is the logical AND. Note also that the coordinates between images and arrays are inverted in matlab.