Segmenting objects from a threshold image

2 views (last 30 days)
I have thresholded an image using thresholder app in Matlab and converted the output for an image to black and white. AS a result I got the following output,
Now what I need to do is to isolate the specific type of square in the thresholded image from above image: I want my output to be as follows
I have tried bwareafilt(mask, [minArea maxArea]); command but that dosen't give optimal solution, more on that is here https://www.mathworks.com/matlabcentral/answers/676728-how-to-implement-helping-functions-for-counting-lego-blocks-in-image
Please follow comment section in above link to understand the problem of using bwareafilt command.
Is there anyone who knows solution for how to tackle this problem.
Thank You in anticipation.
  3 Comments
Adnan Khan
Adnan Khan on 5 Dec 2020
It is the size that distinguishes it from others. It can be placed anywhere in other images. Below is the actual image, which was first thresholded using thresholder app, and the result generated from it is changed into black and white.
here I am not posting the code which was auto generated by matlab thresholder app; the function's name used for thresholding blue blobs is createMaskBlue() which efficiently threshold's the blue blobs.
code is:
[maskBlue, maskedRGBIm] = createMaskBlue(I);
maskBlue = bwareafilt(maskBlue, [26000 75000]);
% Count number of Red blobs
[labeledImage, numB] = bwlabel(maskBlue);
imshow(labeledImage)
The labeled image is the first one posted in question
and rgb image is the following
Adnan Khan
Adnan Khan on 5 Dec 2020
The blue blobs here are of different sizes as you may see in rgb image. Based on the size, I want the smallest blob's count, not only for this image but for more such images also, where the smallest blob can be placed in different positions. I hope my question is clear now.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 5 Dec 2020
"Based on the size, I want the smallest blob's count" <== If by count you mean area in pixels of the smallest blob in the segmented image, you can do this:
smallestBlob = bwareafilt(maskBlue, 1, 'smallest');
areaInPixels = nnz(smallestBlob)
  3 Comments
Adnan Khan
Adnan Khan on 5 Dec 2020
and by count I mean, to count number of blobs present in the image of the smallest size.
Image Analyst
Image Analyst on 5 Dec 2020
With an image that is millions of pixels, it's highly unlikely that you will have two blobs with the same area. If you see that happening, I'd like to see the original image and mask image. However there is a way to do it.
% Measure areas of all blobs:
props = regionprops(mask, 'Area')
% Get all the areas into a double vector
allAreas = [props.Area];
% Find the smallest area, in pixels.
minArea = min(allAreas)
% Count the number of blobs that have the minArea exactly.
blobCount = sum(allAreas == minArea) % Will be 1 in almost every case unless you've synthetically created an image with known areas.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!