Comparing an occurance of colors between 2 images

3 ビュー (過去 30 日間)
dila suay
dila suay 2023 年 2 月 7 日
回答済み: prasanth s 2023 年 2 月 8 日
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 件のコメント
DGM
DGM 2023 年 2 月 7 日
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 2023 年 2 月 7 日
編集済み: DGM 2023 年 2 月 7 日
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.

サインインしてコメントする。

採用された回答

prasanth s
prasanth s 2023 年 2 月 8 日
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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by