Balance brightness and contrast between two images I have two different images for the same object. Each one was taken from different device (scanner, mobile camera, professional camera ..etc).
the process of  registeration one of them to the other was done correctly. Now I need to do a subtraction between to them for comparison purpose. The problem that is the contrast and brightness are pretty different. I tried these two methods (I tried them on the whole image(globally) and on pairs of the two images (locally)):


Gray-Scale method:

ImageB= ImageA * (Mean_Of(ImageA)/Mean_Of(ImageB));
Color method:
Convert it to HSV then:

ImageB[S]= ImageA[S] * (Mean_Of(ImageA[S])/Mean_Of(ImageB[S]));
ImageB[V]= ImageA[V] * (Mean_Of(ImageA[V])/Mean_Of(ImageB[V]));


return to RGB again

However, the two methods did not give me the results I want. They are good and work well on many images but not in all cases.

Any suggestions?
Many thanks
The images you are working with are probably with gamma compensation, you should remove it first by applying inverse gamma.
More accurately, each camera has its own tone curve mapping. You can build a lookup table that converts one from another.
After you removed the non-linearity source, (whether you used method 1 or method 2), as a first order approximation, you can map colors from different cameras by using a 3x3 matrix applied on RGB. 


Now you can compare your images.