# Covariance between the individual channels of an image

To what extent do the pixel values within pairs of channels in this image vary together?

```import numpy as np from PIL import Image from sklearn.preprocessing import MinMaxScaler def mean(x): return sum(x) / len(x) def cov(x, y): mean_x, mean_y = mean(x), mean(y) return sum([(xi - mean_x) * (yi - mean_y) for xi, yi in zip(x, y)]) / (len(x) - 1) mms = MinMaxScaler(feature_range=(-1, 1)) im = Image.open('busan_night_scene.jpg') imnp = np.array(im) channels = [mms.fit_transform(imnp[:,:,i]).ravel() for i in range(3)] channel_names = ['red', 'green', 'blue'] combinations = [(0, 1), (1, 2), (2, 0)] for ch1, ch2 in combinations: covariance = cov(channels[ch1], channels[ch2]) print('Covariance between %s and %s channel: %.5f (also with numpy? %s)' % ( channel_names[ch1], channel_names[ch2], covariance, np.allclose(np.cov(channels[ch1], channels[ch2])[0][1], covariance) )) """ Covariance between red and green channel: 0.20282 (also with numpy? True) Covariance between green and blue channel: 0.09542 (also with numpy? True) Covariance between blue and red channel: 0.04822 (also with numpy? True) """```

There is almost no pronounced relationship (0.04822) between the blue and red channels and a slightly positive one (0.20282) between the red and the green channels in this image.