问题
I have this image:
Where the red dots are coordinates that divide the different letters of this Arabic word segment.
I want to detect the dots above or below the areas between the dots.
The dots are (from left to right) = [81,183;80,217;83,275;83,314]
Now there is a dot above the letter between [81,183]
and [80,217]
. Similarly there are dots above section between [80,217]
and [83,275]
and dots below region [83,275]
and [83,314]
.
What I want to do is suppose a dot is detected above a coordinate then that coordinate must be deleted. Would it be possible to detect these in Matlab?
edit: Here is the original image
The first image is just a crop showing my region of interest
回答1:
You can extract the coordinates of the individual objects with regionprops
Here is an example implementation:
im=rgb2gray(imread('http://i.stack.imgur.com/jic1X.jpg'));
P=regionprops(~im2bw(im), 'All');
Areas=cell2mat({P.Area});
Centroids=cell2mat({P.Centroid}');
Select only the points that have an area larger that 10 but smaller than 100:
Coord=Centroids(Areas< 100 & Areas > 10,:);
Monitor the dots found:
imshow(im);
hold on
for k=1:length(Coord)
plot(Coord(k,1), Coord(k,2), 'ro');
hold on
end
Result:
You can then sort the points with something like:
Aboves=Coord(Coord(:,2) < 80,:);
Belows=Coord(Coord(:,2) > 80,:);
From here, there are many ways of solving your problem, one option is the following:
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
The result is a vector DetectDots
with value [1,1,-1]
in this case that indicates that there are dots above between the two first point, and between the second and third point, and dots below between the third and last point of the vector 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.
来源:https://stackoverflow.com/questions/23201042/how-to-detect-a-point-above-and-below-a-region