Search code examples
pythonimage-processingscikit-image

Compute variance of image


I want to compute a variance of laplacian of an image without using opencv since opencv is quite huge. So, I want to use any other library that has smaller size.

Here is my current code. It works perfectly but using opencv-python-headless:

import cv2

image = cv2.imread("images/test.png")
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

laplacian = cv2.Laplacian(image_gray, cv2.CV_64F).var()
print(laplacian)

How can I get the same result (same laplacian value) using any other library/libraries that has smaller size?


Solution

  • You might consider using scipy instead with its laplacian function.

    from scipy import ndimage #To compute the laplacian
    import imageio #To load the image as a numpy array
    import numpy as np #To convert it to gray
    
    #A function to convert your rgb image to gray
    convert_to_gray = lambda rgb : np.dot(rgb[... , :3] , [0.299 , 0.587, 0.114]) 
    
    image = imageio.imread("images/test.png")
    image_gray = convert_to_gray(image)
    
    laplacian = ndimage.laplace(image_gray).var()
    print(laplacian)