Luckily, I came up with a solution after studying the details of image matrices. Here is what I got: I just add all the greyscale values of each of, say two, greyscale images and divide this sum by the number of pixels which have non-zero greyscale values (since the transparent background is excluded from the matching routine). This gives the mean greyscale value for a pixel of the foreground in an image. The prerequisite (which I need for my research) is that the images must share the same dimensions r1 x c1 = r2 x c2 and that they have uniformly transparent background.
This loop sums all pixel greyscale values for all pixels with a non-255 (alternatively non-zero) greyscale value, giving the total foreground brightness. r = rows, c = columns, I1 = Image 1, I2 = Image 2; of course, one has to read in both images via the "imread" operation.
for i = 1:r
n = 1;
while n <= c
if I1(i,n,1)<255
B1 = [B1, I1(i,n,1)];
end
if I2(i,n,1)<255
B2 = [B2, I2(i,n,1)];
end
n=n+1;
end
end
TotBI1 = sum(B1);
TotBI2 = sum(B2);
Here, the number of pixels is counted which have a non-zero (or non-255) greyscale value for both of the two images (as picked in the loop above):
PixNr1 = length(B1);
PixNr2 = length(B2);
Determining the average brightness per pixel which has a non-zero (or non-255) greyscale value (this can also be done with the "mean" operation):
BPixAvg1 = TotBI1/PixNr1;
BPixAvg2 = TotBI2/PixNr2;
Finally, the modification of all the pixels with non-255 greyscale values by DeltaB which is the equalization (or compromise) greyscale value; the image foreground with darker greyscale values gets brighter by DeltaB, and the lighter image foreground gets darker by DeltaB:
DeltaB = (BPixAvg1 - BPixAvg2)/2;
for i = 1:r
n=1;
while n<=c
if I1(i,n,1)<255
I1(i,n,1:3) = I1(i,n,1:3)-DeltaB;
end
if I2(i,n,1)<255
I2(i,n,1:3) = I2(i,n,1:3)+DeltaB;
end
n=n+1;
end
end
end
I will try to generalize the code for all sorts of images, since some folks who work on some visual psychophsical experiments with MATLAB PsychToolbox need carefully controlled objective parameters, like brighntes of paired images (like for binocular rivalry).
Moreover, though sharing the same average greyscale value per pixel, a bigger object in foreground of the one image can appear darker (or lighter) than a smaller object of the other image. For this, I will include also some "spacing" factor which weights the object size and it's apparent brightness ...