参数 `image` 必须是二维数组
The parameter `image` must be a 2-dimensional array
我要计算一些纹理的共现矩阵,下面你会看到代码,通常当我在cv2.imread()中硬编码一个地址时,不会出现错误,但现在我已经使用一个变量(train_image),我得到错误,'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)
这是我得到的错误:
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
感谢任何帮助或提示,谢谢!
您需要将彩色图像转换为灰度图像
im = cv2.imread(train_image)
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ngcm= greycomatrix(im, [1], [0], 256, symmetric=False, normed=True)
您需要将图像更改为灰度图像。
如果您使用枕头:
img = io.imread(image, as_gray=True)
如果您使用的是 openCV:
img = cv2.imread(image)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
您需要使用以下代码将彩色图像更改为灰色图像。
image = cv2.imread(img,0) #0 indicates gray scale image
我要计算一些纹理的共现矩阵,下面你会看到代码,通常当我在cv2.imread()中硬编码一个地址时,不会出现错误,但现在我已经使用一个变量(train_image),我得到错误,'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)
这是我得到的错误:
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
感谢任何帮助或提示,谢谢!
您需要将彩色图像转换为灰度图像
im = cv2.imread(train_image)
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
ngcm= greycomatrix(im, [1], [0], 256, symmetric=False, normed=True)
您需要将图像更改为灰度图像。
如果您使用枕头:
img = io.imread(image, as_gray=True)
如果您使用的是 openCV:
img = cv2.imread(image)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
您需要使用以下代码将彩色图像更改为灰色图像。
image = cv2.imread(img,0) #0 indicates gray scale image