1

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?

Muskan Bansal
  • 79
  • 2
  • 13
  • 1
    Converting between OpenCV is skimage isn't hard, but you need to be aware of some subtleties. I recommend using the following: `from skimage import io, color; img = io.imread('leaf2.jpg'); img = color.rgb2gray(img)` – Stefan van der Walt Jul 05 '18 at 06:07

2 Answers2

3

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).

Sunreef
  • 4,452
  • 21
  • 33
  • I tried doing that but it gives a matrix with all entries 0. Will that give correct contrast? – Muskan Bansal Jul 04 '18 at 11:55
  • @MuskanBansal Which matrix has only 0s and how do you know it ? If your matrix is big, the `print` function will only show you a small subset of the matrix. – Sunreef Jul 04 '18 at 12:12
0

you can directly load in grayscale by doing img=cv2.imread('leaf2.jpg',0)

dboy
  • 1,004
  • 2
  • 16
  • 24