How can I color an image?

Hello, I'm trying to segment an image using the DBScan algorithm, but the result is black and white. How can I convert the result into a colored image? I think, I need to use the reshape function, but I couldn't figure it out. I have little image processing knowledge. Thanks for the help.
rgbImage = imread('1.jpg');
imshow(rgbImage);
rChannel = rgbImage(:, :, 1);
gChannel = rgbImage(:, :, 2);
bChannel = rgbImage(:, :, 3);
Img = double([rChannel(:), gChannel(:), bChannel(:)]);
% m = Idx, n = Indicator for core points -> (X, Epsilon, MinPTS, Distance)
[Idx, n] = dbscan(Img, 0.3, 50,'Distance','chebychev');
imshow(Idx);

 Accepted Answer

Image Analyst
Image Analyst on 28 Mar 2023
I don't think kmeans is a good way to segment a color image. I attach a demo as proof. You'd be better off using discriminant analysis. See attached demo "Classify_RGB_Image.m". I'm not sure about dbscan but I think it might even be worse than kmeans.

4 Comments

MB
MB on 28 Mar 2023
Edited: MB on 29 Mar 2023
Thank you very much for your reply. As you know, both methods (K-means, DBScan) are used in academic studies. Density-based clustering algorithms, such as K-Means, DBScan, BIRCH can be adapted for segmentation. I just wanted to try them for our project. In fact, there are many image segmentation techniques in Matlab. imsegkmeans function performs segmentation using K-means. I will definitely try DA. The results you shared sir seem quite successful.
Yeah, you can try all of them. In the end what one is best is what you say is best. The reason I say dbscan might not work well is that it tends to go off and grab another cluster, even if it should be another, separate cluster just if it happens to be connected by a thin tendril of RGB coordinates in the plot of RGB in RGB-space.
Here is another one you can try:
MB
MB on 29 Mar 2023
Edited: MB on 29 Mar 2023
Thanks again sir. The results of this technique seems better than DA. Do we just intuitively understand that the image is well segmented? For example, the quality of the clustering can be measured with metrics such as Calinski-Harabasz. Is there a metric used for this?
I'm not sure any mathematical metric would necessairly tell you one method is better than another. Sure, one method may give a "better" value than another but does that necessarily mean it segmented the colors more like how a real human would do it? I think in the end it comes down to a judgment call.

Sign in to comment.

More Answers (1)

Ergin Sezgin
Ergin Sezgin on 28 Mar 2023
Hello,
Reshape function only changes the size of an array but keeps the number of elements same. For a color image in RGB color space, you need to constitute three color channels, same sized matrices with your grayscale image. The problem is that when you convert your color image into a grayscale image, the color information is permanently lost and it is not possible to automatically recolorize. You can manually colorize it with a photo editing software but even in that case it will not be the same.
However, in your case the output doesn't seem to be an image. Instead it's an index array, representing which cluster or segment the pixels in your image belong to.

2 Comments

MB
MB on 28 Mar 2023
Edited: MB on 28 Mar 2023
Cevap için teşekkürler. Bad news. The original picture is a picture of a flower. It is possible to see a black and white silhouette of a flower. However, it will be difficult to define the objects, if there were more. I just found a code for K-means clustering based image segmentation. I will try to modify it for my problem.
[m, n] = kmeans(Img, nClasses);
m = reshape(m, size(rgbImage, 1), size(rgbImage, 2));
n = n/255;
clusteredImage = label2rgb(m, n);
imshow(clusteredImage);
Like I said in my answer below, kmeans, and probably dbscan, is not a good color segmentation method. Maybe you should try rgb2ind() if you want something simple.

Sign in to comment.

Products

Release

R2022b

Asked:

MB
on 28 Mar 2023

Commented:

on 29 Mar 2023

Community Treasure Hunt

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

Start Hunting!