Jalo
Jalo

Reputation: 1129

How I calculate the covariance between 2 images?

I am working on image processing with python. Specifically, I am trying to implement an algorithm called Structural similarity index measure (SSIM) between two images (x and y), which I extracted from this article this article. In that formula I need the covariance between the two images. I know how to calculate the covariance between two vectors, but I don't know how to calculate the covariance of two matrices (I assume each image is a matrix of pixels), anyone can help me? I tried using the numpy function numpy.cov(x,y) [doc] but I have a large 3-D matrix, and I actually need a scalar value

Upvotes: 2

Views: 9492

Answers (3)

n13
n13

Reputation: 23

import cv2 
import numpy as np
from PIL import Image, ImageOps #PIL = pillow
from numpy import asarray

'''read image via PIL -- in opencv it equals to img1 = cv2.imread("c1.jpg") '''
img1 = Image.open('c1.jpg')
img2 = Image.open('d1.jpg')

gimg1 = ImageOps.grayscale(img1) #convert to grayscale PIL 
gimg2 = ImageOps.grayscale(img2)

#asarray() class is used to convert PIL images into NumPy arrays
numpydata1 = asarray(gimg1) 
numpydata2 = asarray(gimg2) 

print("Array of image 1: ", numpydata1.shape)
print("Array of image 2: ", numpydata2.shape)

#grayscale images are saved as 2D ndarray of rows(height) x columns(width)
height = int(numpydata2.shape[0] * 
         (numpydata1.shape[0]/numpydata2.shape[0] ) ) 
width = int(numpydata2.shape[1] * (numpydata1.shape[1]/ 
         numpydata2.shape[1] ) )

#print(width)
#print(height)

#when using resize(), format should be width x height therefore, create a new image called new and set it to w x h
 
new = (width, height)

#resize image so dimensions of both images are same
resized = cv2.resize(numpydata2, new, interpolation = cv2.INTER_AREA)

print("Array of resized image 2: ", resized.shape)

def Covariance(x, y):

    xbar, ybar = x.mean(), y.mean()

    return np.sum((x - xbar)*(y - ybar))/(len(x) - 1)

print( Covariance(numpydata1, resized))



'''#Alternative Method - convert grayscale image to array using np.array 
np_img1 = np.array(gimg1)
np_img2 = np.array(gimg2) 
'''

Upvotes: 0

Minoar Hossain
Minoar Hossain

Reputation: 51

Using python we can calculate covariance between two images with following way

import numpy as np

def Covariance(x, y):
    xbar, ybar = x.mean(), y.mean()
    return np.sum((x - xbar)*(y - ybar))/(len(x) - 1)

now take two images img1,img2 and call the function and print it as given way

print( Covariance(img1,img2))

Upvotes: 3

Honesta
Honesta

Reputation: 1504

Check this library: pyssim. Might be what you're looking for.

Upvotes: 1

Related Questions