Creating mask that take only area with specific number of pixel using convolution

4 views (last 30 days)
I'm trying to make binary mask that take only areas with specific number of pixel,using convolution I know how to calculate number of pixels,but I cant figure out how to convolve the mask over the image and make it return only the areas within range of pixels and ignore the rest.. I tried many times, but nothing works.. I would greatly appreciate any help

Answers (1)

Image Analyst
Image Analyst on 3 Dec 2014
Let's say you have a binary image called "binaryImage", and let's say you want to make another binary image called "mask" that is "true" only when the scanning window has 8 to 15 "true" pixels in binaryImage. So, first you'd count the number of pixels in the window, let's say the window is 5 by 5.
countImage = conv2(double(binaryImage), ones(5), 'same');
Now find pixels where the count is in the range 8 to 15, inclusive
mask = countImage >= 8 & countImage <= 15;
That's it. You're done. mask is the final resulting binary image.
  2 Comments
Reema
Reema on 3 Dec 2014
Thanks for your answer, I really appreciate it, but it didn't give me the result I expected, the image I'm working on include connected objects, I want to create mask to extract the smallest objects on each end,as in the attached image
Image Analyst
Image Analyst on 3 Dec 2014
Would have been nice to know that from the beginning. Advice on image processing is always better if you have an image to look at rather than guessing "blind".
You're going to have to separate them first. Either call watershed() or do repeated imerode() until they split apart.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!