Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to do histogram comparison in this case?

As I don't know the limit of histogram comparison ,I decided to ask the wonderful community.
I have some images and I want to group them according to its similarity.every image has a unique colored lines starting from the top down to the bottom of the image,so what I did is taking a vertical projection through those colored lines and make a histogram for this projection for every image.It works well as two similar images should have two similar histogram (i.e both go up and down almost exactly the same rounds) as a human I can decide that both histograms are similar according to that uniquely feature even if one histogram doesn't have high values of its peaks (i.e that is a histogram of a faint image)

The images with its corresponding histograms:
....................1st http://imageshack.us/a/img571/948/onelg.jpg..................................................2nd http://imageshack.us/a/img255/642/twor.jpg........................................ 3rd http://imageshack.us/a/img577/3931/threeaw.jpg................ 1sth http://imageshack.us/a/img823/4343/onehq.png 2ndh http://imageshack.us/a/img687/3738/twoht.png 3rdh http://imageshack.us/a/img43/9996/threeh.png
.........................................................................2nddarker http://imageshack.us/a/img690/7817/twodark.jpg
.................................................2nddarkhist http://img20.imageshack.us/img20/6070/darkerh.png

I converted the two images to gray-scale , then I made a vertical projection of the image converting it to a histogram and As you may notice the first two histograms are similar but the third is kinda different that's because it has a switched colored lines.

Note:-
(1) considering the first two histograms .Although the highest beak is nearly 12, it isn't always the case with some images are darker or fainter but in the end it really gives the same morphology of the histogram if I can say that as you can notice the last histogram is of a darker image of the second one.

My question is :Is it safe to do any kind of histogram comparison??Does histogram comparison mean that I can decide if two histograms have the same peaks for example? or may be in others words does histogram comparison tell me if two histograms have the same morphology??What is the best library or method to do this kind of comparison in python??

Update:-
(1) As a reply @PepperoniPizza and @FedericoCristina ,In my case totally different images will surely have different histograms (you can rely on that) every image has a unique histogram morphology(i.e unique peaks) and the number of peaks varies from one image to another but two similar image should have the same number of peaks (in other words> if you look at their histogram, you can tell they kinda look like the same( lookat the 1st,2nd and the last histogram to see what I mean.
(2) To be clear about this I really don't want a solution of how I can group my images but I'm paying a lot of intention of how I can decide that two histograms have similar shape or morphology as a general case!!!!
(3) I know about cv2.CompareHist() but I don't know if it is a proper method to compare my histograms as I don't know how cv2.CompareHist() works(i.e I don't know on what base they do comparison) and actually cv2.CompareHist() has 4 types of histogram comparison which I don't know what is the best or even what is the alternative method to do such comparison.
(4) as a reply @remi,here is the point:-I had already 3 libraries in which they support histogram comparison,and it's kinda ambiguous -for me at least- to know what they meant by histogram comparison as some just do like h1-h2 and then calcuate(MSE)so all it does is comparing values not the shape these values form in the histogram.So as I'm not that good at histograms and their math-work, I'm wondering if I really can compare histograms such a way.

Thanks

like image 736
Someone Someoneelse Avatar asked Oct 06 '22 02:10

Someone Someoneelse


2 Answers

Is it safe to do any kind of histogram comparison?

Nope, it's not enough. You could use histogram comparison in order to discard less similar ones, but as @PepperoniPizza said, two totally different images could have the exact same histogram.

If you need to group images based on similarity, then you need to implement a 2D Mean Squared Error (MSE) (or Mean Absolute Error (MAE)) algorithm, which is simply a measure of difference between two images, where 0 means they are equal.

What is the best library or method to do this kind of comparison in python?

Implementing these functions is very easy (involves a couple of FOR statements calculating the error or diference between two pixels, one from image A, and one from image B). You can use Python OpenCV or even Pyhon Imaging Library.

like image 127
Federico Cristina Avatar answered Oct 10 '22 02:10

Federico Cristina


As far as I understand, you are not really computing the histograms of the image, since you do a projection. You need a descriptor that takes into account not only the frequency of the colors in your image but also their structure. You could have a look at MPEG7 ColorStructure or ColorLayout descriptors. There is a reference implementation in C++ of the MPEG7 descriptors, which I remember was quite painful to get to work. ColorStructure is the easiest to implement, and you can compared color structure histograms based on their euclidean distance: the shortest the distance btw the descriptors, the most similar the images are (supposed to be).

like image 28
remi Avatar answered Oct 10 '22 03:10

remi