Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
808 views
in Technique[技术] by (71.8m points)

matlab - comparing two image using histogram

I want to find the histogram of two images and find the similarity using Euclidean distance. I'm trying to use the imhist command but it is giving 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Indeed, histograms are meant to represent the repartition of tonal values for one single channel. Color images are often 3-channels images (Red, Green, Blue in most cases).

Ghaul's method should work quite correctly. If you want to be more precise, you can extract each channel and compute its histogram:

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

You are now able to define an evaluation fonction based on the 3 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

You can therefore put the emphasis on one color or the other in your distance definition. That could be useful if the image you want to test has a specific color.

This is an alternative to Ghaul's method, but its equivalent would be to set Alpha, Beta and Gamma as "0.2989 * R + 0.5870 * G + 0.1140 * B", as Andrey stated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...