Equalizing luminance, brightness and contrast for a set of images

前提是你 提交于 2019-12-10 20:33:30

问题


I use MATLAB for a series of experiments where each eye gets stimulated with a different motiv from the images (binocular rivalry), like on the one is a bottle, on the other a watch. There is a Toolbox to adjust the luminance and spatial frequency (Shine toolbox), but that does not work for images where the background is simply transparent, like .png/.tif/.bmp etc. Moreover, the background which shall stay transparent gets included in the analysis and matching routine so that the objects in the center of the image still are not matched perfectly.

Is there a straightforward method in MATLAB (or GIMP) to match the three quantities (luminance, brightness, contrast) in a set of greyscale-images? In GIMP I tried to do it via "Levels", but with unconvincing outcomes, since the histograms of each image are pretty different. Is it possible to setup a predefined template histogram according to which all other images can be matched to get them the same for the three quantities?

Thanks in advance! Dalibor


回答1:


In Matlab, if you only want to match the non-transparent part of the image, you can extract the non-transparent part into a 1D-image, pass that to the toolbox function, and then reconvert the image back to its original shape.

Say you have an image with RGBA information, where A=1 is fully opaque. Then you can transform the image like this:

opaqueIdx = imgRGBA(:, :, 4) == 1; 
imgOpaque = zeros(sum(opaqueIdx(:)), 1, 3); 
for color = 1:3
   myColor = imgRGBA(:, :, color); 
   imgOpaque(:, :, color) = myColor(opaqueIdx); 
end

imgOpaque can then be adjusted, after which you overwrite the opaque parts in the original image with the adjusted values.




回答2:


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 ...



来源:https://stackoverflow.com/questions/13892188/equalizing-luminance-brightness-and-contrast-for-a-set-of-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!