Detect black dots from color background

前端 未结 3 870
攒了一身酷
攒了一身酷 2021-02-01 23:04

My short question

How to detect the black dots in the following images? (I paste only one test image to make the question look compact. More images can be found →here←

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 23:35

    Here is an extraodinarily simplified version, that can be extended to be full RGB, and it also does not use the image procesing library. Basically you can do 2-D convolution with a filter image (which is an example of the dot you are looking for), and from the points where the convolution returns the highest values, are the best matches for the dots. You can then of course threshold that. Here is a simple binary image example of just that.

    %creating a dummy image with a bunch of small white crosses
    im = zeros(100,100);
    numPoints = 10;
    
    % randomly chose the location to put those crosses
    points = randperm(numel(im));
    % keep only certain number of points
    points = points(1:numPoints);
    % get the row and columns (x,y)
    [xVals,yVals] = ind2sub(size(im),points);
    
    for ii = 1:numel(points)
       x = xVals(ii);
       y = yVals(ii);
       try
           % create the crosses, try statement is here to prevent index out of bounds
           % not necessarily the best practice but whatever, it is only for demonstration
           im(x,y) = 1;
           im(x+1,y) = 1;
           im(x-1,y) = 1;
           im(x,y+1) = 1;
           im(x,y-1) = 1;
       catch err
       end
    end
    % display the randomly generated image
    imshow(im)
    
    % create a simple cross filter
    filter = [0,1,0;1,1,1;0,1,0];
    figure; imshow(filter)
    
    % perform convolution of the random image with the cross template
    result = conv2(im,filter,'same');
    
    % get the number of white pixels in filter
    filSum = sum(filter(:));
    
    % look for all points in the convolution results that matched identically to the filter
    matches = find(result == filSum);
    
    %validate all points found
    sort(matches(:)) == sort(points(:))
    % get x and y coordinate matches
    [xMatch,yMatch] = ind2sub(size(im),matches);
    

    I would highly suggest looking at the conv2 documentation on MATLAB's website.

提交回复
热议问题