The parameter `image` must be a 2-dimensional array

shafigh picture shafigh · Feb 10, 2016 · Viewed 12.7k times · Source

I am going to compute co-occurance matrix of some textures and in following you see the code, normally when I hardcode one address in cv2.imread(), no error arises, but now that I have used one variable(train_image), I got error, that 'The parameter image must be a 2-dimensional array'.

train_image=[]
train_images = cvutils.imlist(r"data/lbp/train/")
#n_training_samples = len(train_images)
train_dic = {}
with open('data/lbp/train_label.txt', 'rb') as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    for row in reader:
        train_dic[row[0]] = int(row[1])

X_train = []
X_name = []
y_train = []
#z_test = []
for train_image in train_images:
    im = cv2.imread(train_image)
    ngcm= greycomatrix(im, [1], [0], 256, symmetric=False, normed=True)
    contrast = greycoprops(ngcm, 'contrast')
    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    radius = 2
    no_points = 8 * radius
    lbp = local_binary_pattern(im_gray, no_points, radius, method='uniform')
    #classifier = svm.SVC(gamma=0.001)
    #classifier.fit(data[:n_samples / 2], digits.target[:n_samples / 2])
    x = itemfreq(lbp.ravel())
    hist = x[:, 1]/sum(x[:, 1])
    X_name.append(train_image)
    X_train.append(hist)
    y_train.append(train_dic[os.path.split(train_image)[1]])
model = LinearSVC(C=100.0, random_state=50)
model.fit(X_train, y_train)

this is the error I got :

ValueError                                Traceback (most recent call last)
<ipython-input-20-9c879ecfb9e6> in <module>()
      5 for train_image in train_images:
      6     im = cv2.imread(train_image)
----> 7     ngcm= greycomatrix(im, [1], [0], 256, symmetric=False, normed=True)
      8     contrast = greycoprops(ngcm, 'contrast')
      9     im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

d:\Anaconda2\lib\site-packages\skimage\feature\texture.pyc in greycomatrix(image, distances, angles, levels, symmetric, normed)
     90 
     91     """
---> 92     assert_nD(image, 2)
     93     assert_nD(distances, 1, 'distances')
     94     assert_nD(angles, 1, 'angles')

d:\Anaconda2\lib\site-packages\skimage\_shared\utils.pyc in assert_nD(array, ndim, arg_name)
    163         ndim = [ndim]
    164     if not array.ndim in ndim:
--> 165         raise ValueError(msg % (arg_name, '-or-'.join([str(n) for n in ndim])))

ValueError: The parameter `image` must be a 2-dimensional array

Any help or hint are appreciated,thanks !

Answer

duyvk picture duyvk · Dec 7, 2016

you need to convert the color to grey image

im = cv2.imread(train_image)
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ngcm= greycomatrix(im, [1], [0], 256, symmetric=False, normed=True)