Comparing two images using a histogram

I want to find a histogram of two images and find similarities using Euclidean distance. I try to use the imhist command, but it gives the following error:

 Error using ==> iptcheckinput Function IMHIST expected its first input, I or X, to be two-dimensional. 

my code is as follows:

 % read two images Im1 = imread('1.jpg'); Im2 = imread('2.jpg'); % convert images to type double (range from from 0 to 1 instead of from 0 to 255) Im1 = im2double(Im1); Im2 = im2double(Im2); % Calculate the Normalized Histogram of Image 1 and Image 2 hn1 = imhist(Im1)./numel(Im1); hn2 = imhist(Im2)./numel(Im2); % Calculate the histogram error f = sum((hn1 - hn2).^2); f; %display the result to console 
+6
matlab
source share
2 answers

Indeed, histograms are intended to represent recalculation of tonal values ​​for a single channel. Color images are often 3-channel images (red, green, blue in most cases).

The Gaul method should work correctly. If you want to be more precise, you can extract each channel and calculate its histogram:

 Red1 = Im1(:, :, 1); Green1 = Im1(:, :, 2); Blue1 = Im1(:, :, 3); HnBlue1 = imhist(Blue1)./numel(Blue1); 

Now you can define an estimate based on three Euclidean distances (1 for each channel):

 FBlue = sum((HnBlue1 - HnBlue2).^2); FRed= sum((HnRed1 - HnRed2).^2); ... F = Alpha*FBlue + Beta*FRed + Gamma*FGreen //as an example 

Thus, you can focus on one color or another in determining distance. This can be useful if the image you want to test has a specific color.

This is an alternative to the Gaul method, but its equivalent would be to set Alpha, Beta and Gamma as "0.2989 * R + 0.5870 * G + 0.1140 * B," as Andrei said.

+6
source share

I assume that your images are color images, i.e. have three channels. To reduce them to a single grayscale image channel, do

 Im1 = rgb2gray(Im1); Im2 = rgb2gray(Im2); hn1 = imhist(Im1)./numel(Im1); hn2 = imhist(Im2)./numel(Im2); 

etc..

Alternatively, if you want to work with all color channels, you can stretch your images in vectors before doing imhist, i.e. just do

 hn1 = imhist(Im1(:))./numel(Im1); hn2 = imhist(Im2(:))./numel(Im2); 
+5
source share

All Articles