How to remove excess objects from background?

1 view (last 30 days)
Hi,
I am trying to match the objects from the image on left hand side to the image on right hand side. But I am unable to do it accurately. I have attached the image and the code that I am using. I have also attached the original image. Please help me on how can I go about fixing this. Thanks.
clc
clear all
close all
img= imread("C:\Users\017_14px.tif");
imshow (img)
b=imbinarize (img); %binary image
imshow(b)
img1 = xor(bwareaopen(b,45), bwareaopen(b,1000));
%img1= bwareaopen(b,10);
imshow(img1)
stats = regionprops('table',img1,'Centroid','MajorAxisLength','MinorAxisLength') %object detection
centers = stats.Centroid;
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = diameters/2;
hold on
viscircles(centers,radii); %drawing around the object
hold off
num_obj=height (stats)%finding the number of objects
  4 Comments
Image Analyst
Image Analyst on 9 May 2020
And what does a "fix" look like to you? Ignore 10 of the blobs on the right so now we'll only detect 25 blobs instead of 35? Try thresholding instead of using imbinarize():
thresholdValue = 80; % Whatever. Change until you get what you want.
b = img > thresholdValue; %binary image
You have to give some criteria as to what constitutes a blob? Is it the size? Is it the brightness? The shape?
learningmatlab
learningmatlab on 9 May 2020
Yes, ignore the blobs on right so I detect 25 instead of 35. It is the size and the brightness. As you can see in the right part of image, the grainy background is also getting detected as multiple blobs.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 20 May 2020
There is an algorithm called the Clean algorithm. Basically it works by finding the biggest peak and fitting a Gaussian to it. Then put the fit on the output image and subtract the fit from the input image. Keep going until the peaks are so small that you don't care about them anymore.
There is also another astronomony algorithm - the Groth algorithm. They've even reapplied it to look at spots on whale sharks:

More Answers (1)

Ryan Comeau
Ryan Comeau on 15 May 2020
Hello,
So in order to to fix you problem, there is an algorithm that peforms great for background removal, which is what you probably want here. I unfortunately cannot share it with you as I have programmed it professionally. I can however explain briefly how it works and link some scientific papers for you. The algorithm was desingned by a man called Martin Levesque (paper titled ...iterative background removal...) to remove background from space based image sensors. It involved removing the background of sub images within the image. We calculate the average of the sub image and then subtract the average for that region. You can customize the window size to your purpose. The point is the mean of the region will be removed and the bright pointy objects you have will remain there. Here is a paper for your reading and building of this algorithm:
  1. https://apps.dtic.mil/dtic/tr/fulltext/u2/a479337.pdf
The second thing that you can attempt is the built in median filter function that MATLAB has. You can use a variety of these sequentially as you will want to tune the performance. You will need to select a varying range of window sizes to use. here is some sample code:
image=imread('path/to/image');
image=medfilt2(image,[12 12]);
image=medfilt2(image,[6 6]);
Hope some of these help,
RC

Community Treasure Hunt

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

Start Hunting!