filmov
tv
Extracting Dominant Color of an Image using K-Means Clustering
Показать описание
Prerequisite:
Color Image Segmentation using K-means Clustering Algorithm (with Complete MATLAB Code)
Code(I have added the part which will calculate the max frequency and return the corresponding central pixel):
Note:
----------
In video by mistake , I have iterated the column from index 6(b value in the nested for loop) , but it will be from index 1 , sorry for the inconvenience (below code is the correct one)
clc
clear all
close all
warning off
[filename, pathname] = uigetfile('*.*', 'Pick an Image');
if isequal(filename,0) || isequal(pathname,0)
disp('User pressed cancel')
else
filename=strcat(pathname,filename);
rgbImage=imread(filename);
imshow(rgbImage);
title('Original Image');
k=input('Enter the k value: ');
close
subplot(1,2,1);
imshow(rgbImage);
title('Original Image');
redChannel=rgbImage(:, :, 1);
greenChannel=rgbImage(:, :, 2);
blueChannel=rgbImage(:, :, 3);
data=double([redChannel(:), greenChannel(:), blueChannel(:)]);
numberOfClasses=k;
[m n]=kmeans(data,numberOfClasses);
m=reshape(m,size(rgbImage,1),size(rgbImage,2));
n=n/255;
clusteredImage=label2rgb(m,n);
subplot(1,2,2);
imshow(clusteredImage);
title('Clustered Image');
frequency=[];
temp=0;
for i = 1:k
for a=1:size(rgbImage,1)
for b=1:size(rgbImage,2)
if(m(a,b)==i)
temp=temp+1;
end
end
end
frequency=[frequency temp];
temp=0;
end
figure;
pie(frequency)
colormap([n])%define the color scheme
[ma na]=max(frequency);
disp(n(na,:));
end
Note if you want to visualize the dominant color separately and get the values in command window(which are in double and scaled down by dividing by 255), use the below code--
clc
clear all
close all
warning off
[filename, pathname] = uigetfile('*.*', 'Pick an Image');
if isequal(filename,0) || isequal(pathname,0)
disp('User pressed cancel')
else
filename=strcat(pathname,filename);
rgbImage=imread(filename);
imshow(rgbImage);
title('Original Image');
k=input('Enter the k value: ');
close
subplot(1,2,1);
imshow(rgbImage);
title('Original Image');
redChannel=rgbImage(:, :, 1);
greenChannel=rgbImage(:, :, 2);
blueChannel=rgbImage(:, :, 3);
data=double([redChannel(:), greenChannel(:), blueChannel(:)]);
numberOfClasses=k;
[m n]=kmeans(data,numberOfClasses);
m=reshape(m,size(rgbImage,1),size(rgbImage,2));
n=n/255;
clusteredImage=label2rgb(m,n);
subplot(1,2,2);
imshow(clusteredImage);
title('Clustered Image');
frequency=[];
temp=0;
for i = 1:k
for a=1:size(rgbImage,1)
for b=1:size(rgbImage,2)
if(m(a,b)==i)
temp=temp+1;
end
end
end
frequency=[frequency temp];
temp=0;
end
figure;
pie(frequency)
colormap([n])%define the color scheme
[ma na]=max(frequency);
disp(n(na,:));
figure;
patch([0 0 10 10],[0 10 10 0],n(na,:));
title('Dominant Color');
end
Learn Complete Machine Learning & Data Science using MATLAB:
Learn Digital Signal Processing using MATLAB:
Learn Complete Image Processing & Computer Vision using MATLAB:
🙏🙏🙏🙏🙏🙏🙏🙏
YOU JUST NEED TO DO
3 THINGS to support my channel
LIKE
SHARE
&
SUBSCRIBE
TO MY YOUTUBE CHANNEL
#MATLAB #MachineLearning #DeepLearning #ImageClassification #FeatureExtraction
Color Image Segmentation using K-means Clustering Algorithm (with Complete MATLAB Code)
Code(I have added the part which will calculate the max frequency and return the corresponding central pixel):
Note:
----------
In video by mistake , I have iterated the column from index 6(b value in the nested for loop) , but it will be from index 1 , sorry for the inconvenience (below code is the correct one)
clc
clear all
close all
warning off
[filename, pathname] = uigetfile('*.*', 'Pick an Image');
if isequal(filename,0) || isequal(pathname,0)
disp('User pressed cancel')
else
filename=strcat(pathname,filename);
rgbImage=imread(filename);
imshow(rgbImage);
title('Original Image');
k=input('Enter the k value: ');
close
subplot(1,2,1);
imshow(rgbImage);
title('Original Image');
redChannel=rgbImage(:, :, 1);
greenChannel=rgbImage(:, :, 2);
blueChannel=rgbImage(:, :, 3);
data=double([redChannel(:), greenChannel(:), blueChannel(:)]);
numberOfClasses=k;
[m n]=kmeans(data,numberOfClasses);
m=reshape(m,size(rgbImage,1),size(rgbImage,2));
n=n/255;
clusteredImage=label2rgb(m,n);
subplot(1,2,2);
imshow(clusteredImage);
title('Clustered Image');
frequency=[];
temp=0;
for i = 1:k
for a=1:size(rgbImage,1)
for b=1:size(rgbImage,2)
if(m(a,b)==i)
temp=temp+1;
end
end
end
frequency=[frequency temp];
temp=0;
end
figure;
pie(frequency)
colormap([n])%define the color scheme
[ma na]=max(frequency);
disp(n(na,:));
end
Note if you want to visualize the dominant color separately and get the values in command window(which are in double and scaled down by dividing by 255), use the below code--
clc
clear all
close all
warning off
[filename, pathname] = uigetfile('*.*', 'Pick an Image');
if isequal(filename,0) || isequal(pathname,0)
disp('User pressed cancel')
else
filename=strcat(pathname,filename);
rgbImage=imread(filename);
imshow(rgbImage);
title('Original Image');
k=input('Enter the k value: ');
close
subplot(1,2,1);
imshow(rgbImage);
title('Original Image');
redChannel=rgbImage(:, :, 1);
greenChannel=rgbImage(:, :, 2);
blueChannel=rgbImage(:, :, 3);
data=double([redChannel(:), greenChannel(:), blueChannel(:)]);
numberOfClasses=k;
[m n]=kmeans(data,numberOfClasses);
m=reshape(m,size(rgbImage,1),size(rgbImage,2));
n=n/255;
clusteredImage=label2rgb(m,n);
subplot(1,2,2);
imshow(clusteredImage);
title('Clustered Image');
frequency=[];
temp=0;
for i = 1:k
for a=1:size(rgbImage,1)
for b=1:size(rgbImage,2)
if(m(a,b)==i)
temp=temp+1;
end
end
end
frequency=[frequency temp];
temp=0;
end
figure;
pie(frequency)
colormap([n])%define the color scheme
[ma na]=max(frequency);
disp(n(na,:));
figure;
patch([0 0 10 10],[0 10 10 0],n(na,:));
title('Dominant Color');
end
Learn Complete Machine Learning & Data Science using MATLAB:
Learn Digital Signal Processing using MATLAB:
Learn Complete Image Processing & Computer Vision using MATLAB:
🙏🙏🙏🙏🙏🙏🙏🙏
YOU JUST NEED TO DO
3 THINGS to support my channel
LIKE
SHARE
&
SUBSCRIBE
TO MY YOUTUBE CHANNEL
#MATLAB #MachineLearning #DeepLearning #ImageClassification #FeatureExtraction
Комментарии