Color's histogram and histogram's comparasion.

I've been given a set of images showing different football equipements (t-shirts) with different colors. The idea is, for each image, compute its histogram of RGB colors with N bins, and once computed, compute the similiraty between histograms (using either Chi-Square method or Euclidean distance). I am struggling doing both tasks and I am desperate, so I really hope someone could help me. I'll leave the two functions used down here:
% Histogram computation
function [hist_3d] = Calcular_Histograma_rg(image)
bins = 9;
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);
hist_3d = zeros(bins, bins, bins);
[n m] = size(R);
for i = 1:n
for j = 1:m
r = R(i, j)/ (256/bins) + 1;
b = B(i, j)/ (256/bins) + 1;
g = G(i, j)/ (256/bins) + 1;
if (r > bins)
r = r - 1;
end
if (b > bins)
b = b - 1;
end
if (g > bins)
g = g - 1;
end
hist_3d(r, b, g) = hist_3d(r, b, g) + 1;
end
end
hist_3d = hist_3d ./ (n*m);
hist_3d = hist_3d ./ (n*m);
end
% Similarity using euclidean distance computation
function [similarity] = Calcular_Similitud_rg(hist_org,hist)
[n, m] = size(hist_org);
dist = 0;
for i=1:n
for j=1:m
aux = (hist_org(i, j) - hist(i, j));
dist = dist + (aux*aux);
end
end
similarity = sqrt(dist);
end

1 Commento

Maybe it is better to do it in HSV space. Give the appropiate change as you want.

Accedi per commentare.

Risposte (2)

You're going to have to round r, g, and b because they're not integers and can't be used as indexes.
Don't use image as the name of your variable since it's already a built-in function.
Have you thought about what to do if the two images are not the same size? Maybe you want to normalize the histograms.
Maybe you have implemented the histogram computation and similarity calculation using Euclidean distance. there are a few modifications needed. First, in the Calcular_Histograma_rg function, you have duplicated the normalization step, which should only be done once. Second, you need to adapt the Calcular_Similitud_rg function to handle 3D histograms.
% Histogram computation
function [hist_3d] = Calcular_Histograma_rg(image)
bins = 9;
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);
hist_3d = zeros(bins, bins, bins);
[n, m] = size(R);
for i = 1:n
for j = 1:m
r = floor(R(i, j) / (256/bins)) + 1;
b = floor(B(i, j) / (256/bins)) + 1;
g = floor(G(i, j) / (256/bins)) + 1;
hist_3d(r, b, g) = hist_3d(r, b, g) + 1;
end
end
hist_3d = hist_3d ./ (n * m);
end
% Similarity using Euclidean distance computation
function [similarity] = Calcular_Similitud_rg(hist_org, hist)
[n, m, p] = size(hist_org);
dist = 0;
for i = 1:n
for j = 1:m
for k = 1:p
aux = (hist_org(i, j, k) - hist(i, j, k));
dist = dist + (aux * aux);
end
end
end
similarity = sqrt(dist);
end
image1 = imread('image1.jpg');
image2 = imread('image2.jpg');
hist1 = Calcular_Histograma_rg(image1);
hist2 = Calcular_Histograma_rg(image2);
similarity = Calcular_Similitud_rg(hist1, hist2);

1 Commento

Don't use image as the name of your variable since it's already a built-in function.

Accedi per commentare.

Prodotti

Release

R2022b

Richiesto:

Pol
il 8 Mag 2023

Commentato:

il 9 Mag 2023

Community Treasure Hunt

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

Start Hunting!

Translated by