# Noise removal in image segmentation

19 views (last 30 days)
Stewart Tan on 4 Aug 2019
Commented: Image Analyst on 4 Aug 2019
I'm new to Matlab and I'm currently practicing some basics such as Matlab's own exercise on image segmentation https://www.youtube.com/watch?v=1-jURfDzP1s&t=1450s . The exercise basically in the end, creates bounding boxes around objects in the image and counts the total number of objects.
However, I'm practicing a similar exercise but with a different picture i found in google, but the problem I'm facing is that at the end, a noise particle that was introduced during the threshold adjustment is being considered an object in the image.
Here's the image I'm currently using instead of the one used in the youtube video.
The goal is basically the same which is to count the number of objects in the image, which I've done.
However, there are issues. I'm quite happy that most of the objects get counted but if you look closer to the top right, the table surface is being counted as an object which i didn't want. The noise particle arise when i was thresholding the HSV color space, but if i adjusted till it doesn't appear, then some part of the objects on the table will be split into two (example, the pen top will be one object and the bottom will be another object).
The code I've written is as below:
%% Read in table image file
figure
imshow(table)
%% HSV color space
H = rgb2hsv(table);
H_plane=H(:,:,1);
S_plane=H(:,:,2);
V_plane=H(:,:,3);
% figure;
subplot(2,2,1), imshow(H_plane);
title('H Plane');
subplot(2,2,2), imshow(S_plane);
title('S Plane');
subplot(2,2,3), imshow(V_plane);
title('V Plane');
subplot(2,2,4), imshow(table);
title('Original Image');
%% Threshold HSV color space (the noise particle appears here in the preview)
levelh = 0
levels = 0.26 %original 0.26
levelv = 0.2 %original is 0.2
i1 = im2bw(H_plane,levelh);
i2 = im2bw(S_plane,levels);
i3 = im2bw(V_plane,levelv);
isum = (i1&i2&i3);
subplot(2,2,1), imshow(i1);
title('Hue plane');
subplot(2,2,2), imshow(i2);
title('Saturation plane');
subplot(2,2,3), imshow(i3);
title('Value plane');
subplot(2,2,4), imshow(isum);
title('Combined plane');
%% Complement image and fill the holes
icomp = imcomplement(isum);
ifilled = imfill(icomp, 'holes');
figure, imshow(ifilled);
%%
se = strel('disk', 5);
iopen = imopen(ifilled,se);
imshow(iopen);
%% remove noise
afterClosing = imclose(iopen,se);
imshow(afterClosing)
%% Extract features
Iregion = regionprops(iopen, 'centroid');
[labeled,numObjects] = bwlabel(iopen,4);
stats = regionprops(labeled,'Eccentricity','Area','BoundingBox');
areas = [stats.Area];
eccentricities = [stats.Eccentricity];
%% Use feature analysis to count skittles objects
idxOfSkittles = find(eccentricities);
statsDefects = stats(idxOfSkittles);
figure, imshow(table);
hold on;
for idx = 1 : length(idxOfSkittles)
h = rectangle('Position',statsDefects(idx).BoundingBox,'LineWidth',2);
set(h,'EdgeColor',[.75 0 0]);
hold on;
end
if idx >=1
title(['There are ', num2str(numObjects), ' objects in the image!']);
end
hold off;
Could anyone help me by introducing a method of eliminating the noise particle that was introduced during the threshold adjustment?

Image Analyst on 4 Aug 2019
Well knotholes are certainly objects so it makes sense that it found them. If you just want things that stick up above the surface, maybe you need to use a profilometer or Microsoft Kinect, or the Intel 3-D camera.
If you just throw out small dark things, then you'll also throw out the ear buds.
One of the best ways is to just ake a photo of the table with nothing on it and then subtract that image from your image with objects on the table. Can you do that? Or even better, cover the table with a white sheet of paper.