我将计算一些纹理的共现矩阵,以下是代码。通常情况下,当我在cv2.imread()中硬编码一个地址时,不会出现错误。但是现在我使用了一个变量(train_image),我得到了一个错误,即“参数image
必须是二维数组”。
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)
这是我收到的错误信息:
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
任何帮助或提示都将不胜感激,谢谢!