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