1

I was trying to implement blob detection in Python. I have gone through an implemented Matlab code. But, there is a problem I couldn't understand the code. Is there any function related to this one in OpnenCV, sciktimage, etc. package?

The name of the file of the implemented code is "ExtractNLargestBlobs.m" and the code is-

function binaryImage = ExtractNLargestBlobs(binaryImage, numberToExtract)
try
    % Get all the blob properties.  Can only pass in originalImage in version R2008a and later.
    [labeledImage, numberOfBlobs] = bwlabel(binaryImage);
    blobMeasurements = regionprops(labeledImage, 'area');
    % Get all the areas
    allAreas = [blobMeasurements.Area];
    if numberToExtract > 0
        % For positive numbers, sort in order of largest to smallest.
        % Sort them.
        [sortedAreas, sortIndexes] = sort(allAreas, 'descend');
    elseif numberToExtract < 0
        % For negative numbers, sort in order of smallest to largest.
        % Sort them.
        [sortedAreas, sortIndexes] = sort(allAreas, 'ascend');
        % Need to negate numberToExtract so we can use it in sortIndexes later.
        numberToExtract = -numberToExtract;
    else
        % numberToExtract = 0.  Shouldn't happen.  Return no blobs.
        binaryImage = false(size(binaryImage));
        return;
    end
    % Extract the "numberToExtract" largest blob(a)s using ismember().
    biggestBlob = ismember(labeledImage, sortIndexes(1:numberToExtract));
    % Convert from integer labeled image into binary (logical) image.
    binaryImage = biggestBlob > 0;
catch ME
    errorMessage = sprintf('Error in function ExtractNLargestBlobs().\n\nError Message:\n%s', ME.message);
    fprintf(1, '%s\n', errorMessage);
    uiwait(warndlg(errorMessage));
end

and this file is accessing from the file namely main.m and the code line that is used to access are-

%--------------------blob detection-------------------------------
            mask = ExtractNLargestBlobs(mask2, 1);

The parameter mask2 consists of an Image previously gone through morphological analysis. And the variable mask assigns an image that image is expected to be a clean image that doesn't have the unwanted regions in the image.

Actually, I was implementing the code in Python and I'm not familiar with Matlab. Unfortunately, I couldn't find the above code implemented in Python. Is there any Python library method that could be used to replace the above method? and to overcome the situations like this is it okay to follow the given solution from stackoverflow?

I have found a python implemented code by fmw42 but my code won't suit with the solution despite modifying the code lines - here is the solution

  • Perhaps filter connectedComponentsWithStats() or just get contours after thresholding and filter on area. – fmw42 Jul 16 '23 at 18:22
  • Thank you so much for your valuable suggestion. Please, May I request you to consider elaborating the answer? I couldn't understand. – Mohammad Ishfakur Jul 17 '23 at 16:02
  • Threshold, then get labeled regions using connectedComponentsWithStats(). Then filter on size or other statistics. You should research connected components. See https://docs.opencv.org/4.1.1/d3/dc0/group__imgproc__shape.html#ga107a78bf7cd25dec05fb4dfc5c9e765f – fmw42 Jul 17 '23 at 17:14
  • Thanks a lot; now I can understand more. – Mohammad Ishfakur Jul 17 '23 at 18:50

1 Answers1

0

If you are free to mark down some blobs or ROI on an image and train a simple detector try out dlib.

An example code of how to train the object detector in Python is provided in the project itself (here).

The object detector that you get here is HOG-SVM. In short, it uses the HOG descriptor of regions of interest (positive, you mark these using the imglab tool) and non-interest (negative) samples to train a SVM classifier.

DLIB has been built for tackling real-world machine learning problems hence the inference or testing is very fast. The classifier is pretty robust as well if trained with proper parameters and data.

NOTE: To install dlib you need CMake. For labelling you will need the imglab tool which is included in the project.

tintin98
  • 91
  • 9
  • Why the downvote? If you are downvoting can you please state the reason as I need that to improve my answer if possible. – tintin98 Jul 17 '23 at 07:39
  • Thank you so much for your answer. I couldn't upvote as I'm not allowed to, due to the reputation point. I'm extremely sorry; to answer my question you had to accept a downvote. – Mohammad Ishfakur Jul 17 '23 at 15:58
  • Actually, I was trying to get a possible solution as an alternative to the code mentioned in the question. I was trying to avoid manual annotation and pre-trained models; So, I was looking for a method to perform similar tasks. – Mohammad Ishfakur Jul 17 '23 at 16:17
  • Then follow what @fmw42 has suggested in the comments. The latter approach is same as the code you have linked in your question. The former part is a different approach where instead of finding contour and then area, you straight away find the connected components (potentials blobs) on the image and the OpenCV method connectedComponentsWithStats gives you area of the components to do that. – tintin98 Jul 17 '23 at 16:59
  • Noted! Thank you so much for your support. – Mohammad Ishfakur Jul 17 '23 at 18:50