gpt4 book ai didi

python - 参数 `image`必须是二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:50 24 4
gpt4 key购买 nike

我要计算一些纹理的共现矩阵,下面你会看到代码,通常当我在 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

感谢任何帮助或提示,谢谢!

最佳答案

您需要将颜色转换为灰色图像

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

关于python - 参数 `image`必须是二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35325193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com