# calculate mean or average intensity of an image

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.

calculate mean or average intensity of an image

April 15, 2012 - 02:06
hi, i am able to get 2 dim arrays of the red, green and blue values for each pixel in an image.

how can i calculate the average intensity of the image from these arrays?

thanks

The mean intensity in each of the colour channels is a simple average: loop through each array, add up the values and divide by the number of elements (= no of pixels). However you must be careful that the sums do not overflow. Think carefully about the data type for each sum. If the image has only 8 bits per channel then you might get away with int as the type for the sum, but it will also depend on the number of pixels being summed. You need to estimate the maximum value the sum could possibly have in order to decide what its data type should be. Remember that no exception occurs in Java integer arithmetic when a sum overflows. Also don't forget that the top bit of the sum is used for the sign, otherwise overflow will occur at half the value you expected.

Then an interesting question is what you mean by the mean intensity of a multi-channel image. Is the mean intensity of each colour sufficient or do you need to combine them? You then get into considerations of how much weight to give to each channel and whether you want to mimic the spectral response of the eye. And that depends on what kind of camera the image came from originally: what is its spectral response for each channel? In the absence of any specification about wavelengths the simplest thing is to add the 3 mean intensities.