I have a color image:
Then I applied k-means algorithm and selected this image as the suitable cluster:
I want to apply morphological operations like cleaning border, filling holes and remove small objects using MATLAB but these operations work only on gray-scaled or binary images in MATLAB.
I want to select just the cell in the midst of the image and extract the contour as the final step.
The code is :
NbIm = size(names1,1);
n1 = 1;
n2 = NbIm;
for n = n1:n2
% 1- Lecture de l'image originale
ImPath1 = strcat(DirName1,ImName1(n)); % Chemin de chaque image
Im_originale = imread(char(ImPath1)); % Chargement image
% 1- Appliquer la méthode K-means pour générer TROIS classes
cform = makecform('srgb2lab');
lab_he = applycform(Im_originale,cform);
ab = double(lab_he(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 3;
% repeat the clustering 3 times to avoid local minima
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
pixel_labels = reshape(cluster_idx,nrows,ncols);
% imshow(pixel_labels,[]), title('image labeled by cluster index');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = Im_traiter;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
C1=segmented_images{1};
C2=segmented_images{2};
C3=segmented_images{3};
% 2- Selectionner la classe à traiter
[m ind]=min(cluster_center);
ClusterChoix=ind;
Im1_traiter=segmented_images{ClusterChoix};
end
IMO your image is actually binary as you care about the shape and operations on shape.
Binarize and apply the binary operations, then merge with the original (transfer the colors to the binarized pixels). You may have to extrapolate data at new pixels that appear. There are so-called inpainting techniques available. You might investigate poisson reconstruction, which is similar to a weighted average of the neighboring known pixels.