Comparing an occurance of colors between 2 images

1 view (last 30 days)
Hello Community,
I have 2 groups of images (I put one example from each group in the attachments). I am trying to compare the colors occurances between image1 and image2. When I find the unique number of colors for both image, I ended up way too much colors (which I dont want). Is there any logical way to limit color amount? I have read that clustering by kmeans and using delta-e distance would help me, but I am not able to figure it out.
Simply then, I would like to find:
Which colors are present in the images and their occurance (pixel count)?
Which colors are present in image1 but not image2? How can i show this in colorcloud()?
Thank you so much in advanced
  2 Comments
DGM
DGM on 7 Feb 2023
That really depends what the purpose of finding the colors is. Unless you can assume certain things about the images or unless you classify the colors in the same way for all images, I see no reason to assume that two images containing two identically-colored regions will necessarily have said regions mapped to the same color class when you reduce the palette.
So define what it means for two images to have the "same color"
DGM
DGM on 7 Feb 2023
Edited: DGM on 7 Feb 2023
A naive example:
% some images
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1287660/Image_1.jpg');
B = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1287665/Image_2.jpg');
% get histcounts
Ahist = u8rgbhist(A);
Bhist = u8rgbhist(B);
% generate logical maps of color membership
inA = logical(Ahist);
inB = logical(Bhist);
inAonly = inA & ~inB;
inBonly = ~inA & inB;
inboth = inA & inB;
inneither = ~inA & ~inB;
% number of colors in each set
numinAonly = nnz(inAonly)
numinAonly = 57062
numinBonly = nnz(inBonly)
numinBonly = 40866
numinboth = nnz(inboth)
numinboth = 0
numinneither = nnz(inneither)
numinneither = 16679288
function H = u8rgbhist(inpict)
% generate a 3D histogram of all the colors in a uint8 image
inpict = im2uint8(inpict);
[rows,cols,ch,~] = size(inpict);
if ch~=3
error('U8RGBHIST: expected input to be a RGB image')
end
H = zeros(256,256,256);
for r = 1:rows
for c = 1:cols
idx = inpict(r,c,:) + 1;
H(idx(1),idx(2),idx(3)) = H(idx(1),idx(2),idx(3)) + 1;
end
end
end
There aren't any colors common to the two images.

Sign in to comment.

Accepted Answer

prasanth s
prasanth s on 8 Feb 2023
convert the images to HSV colorspace and use the hue component to clustering.
match the colors using the obtained cluster centers. it will give an approximate matching for closely related colors.
check below code
clear
close all
I1 = imread('Image_1.jpg');
L1=rgb2hsv(I1);
I2 = imread('Image_2.jpg');
L2=rgb2hsv(I2);
n_cluster=20;
hue_1=100*L1(:,:,1);
[idx,C_1] = kmeans(hue_1(:),n_cluster);
K1=reshape(idx,size(hue_1));
[counts_1,bins]=histcounts(K1,n_cluster);
hue_2=100*L2(:,:,1);
[idx,C_2] = kmeans(hue_2(:),n_cluster);
K2=reshape(idx,size(hue_2));
[counts_2,bins]=histcounts(K2,n_cluster);
% find common clusters
C_1=round(C_1);
C_2=round(C_2);
[val,pos]=intersect(C_1,C_2);
for i=1:length(val)
M1=K1==pos(i);
pos2=find(C_2==val(i),1);
M2=K2==pos2;
J1=I1;
J2=I2;
cluster1 = bsxfun(@times, J1, cast(M1, 'like', J1));
cluster2 = bsxfun(@times, J2, cast(M2, 'like', J2));
figure,imshow(cluster1)
figure,imshow(cluster2)
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!