Is there a way to extract the gray background from the image and just to keep the rainbow
Show older comments
I have the following image produced by a diffraction grating on chip producing diffraction pattern.
Is there a way to extract all the color spots while removing the noise of the background?
What I tried was clustering but I don't think is the tool for this task.
Here are all raw images: https://drive.google.com/drive/folders/1B3K4JZd0QXebFceb09hsIKSUl4zCdyzn?usp=sharing
% Read image
img = imread('rainbow.png');
img = im2double(img);
% Reshape image into N x 3 color pixels
pixels = reshape(img, [], 3);
% Number of color clusters (you can adjust this)
k = 5;
% Run k-means clustering on the colors
[idx, C] = kmeans(pixels, k, 'Distance', 'sqeuclidean', 'Replicates', 3);
% Reconstruct clustered image
clusteredImg = reshape(C(idx,:), size(img));
% Show original and clustered version
figure;
subplot(1,2,1);
imshow(img);
title('Original Image');
subplot(1,2,2);
imshow(clusteredImg);
title(sprintf('Color Clusters (k = %d)', k));
Accepted Answer
More Answers (0)
Categories
Find more on Color and Styling in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
