Search code examples
pythonopencvimage-processingscikit-imageglcm

greycomatrix for RGB image


I am trying to find contrast in an image using greycomatrix here is the code:

import cv2
import numpy as np
from scipy import misc
from skimage.feature import greycomatrix, greycoprops


img=cv2.imread('leaf2.jpg')


g=greycomatrix(img, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4])
print (g)

contrast = greycoprops(g, 'contrast')
print(contrast)

Here is the error: "The image must be a 2-D array" How to convert the image to a 2-D array, suitable for the function?


Solution

  • Add img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) after you load the image to make it a one-channel, grayscale image.

    Or you could load it as grayscale directly by doing img = cv2.imread('leaf2.jpg', cv2.IMREAD_GRAYSCALE).