ValueError: Only images with 2 spatial dimensions are supported. If using with color/multichannel im

skimage库 HOG报错

问题描述

我的Python代码为:

des, hog_image = hog(img, orientations=8, pixels_per_cell=(16, 16), 
					cells_per_block=(1, 1), visualize=True, multichannel=True)

此时我的 img 的shape 为 (150, 150)

然后当我在使用 skimage库中的hog时,报了如下错误:

ValueError: Only images with 2 spatial dimensions are supported. If using with color/multichannel images, specify `multichannel=True`.

解决方法

直接把multichannel=True 删掉就行了,简单粗暴~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
抱歉,我之前的代码忽略了图像的通道信息。在使用 `feature.hog` 函数进行特征提取时,如果图像是多通道的(例如RGB图像),需要明确指定 `channel_axis` 参数。以下是修改后的代码: ```python import os import numpy as np from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import accuracy_score from skimage import feature from skimage.io import imread # 读取不戴口罩的照片 X_unmasked = [] y_unmasked = [] unmasked_folder = "X_processed" for folder_name in os.listdir(unmasked_folder): if folder_name.startswith("s"): folder_path = os.path.join(unmasked_folder, folder_name) for file_name in os.listdir(folder_path): image_path = os.path.join(folder_path, file_name) image = imread(image_path) # 使用 skimage.io.imread 读取照片 x_feature = feature.hog(image, orientations=8, pixels_per_cell=(10, 10), cells_per_block=(1, 1), visualize=False, multichannel=True) X_unmasked.append(x_feature) y_unmasked.append(0) # 不戴口罩类别标签为0 # 读取戴口罩的照片 X_masked = [] y_masked = [] masked_folder = "X_masked" for folder_name in os.listdir(masked_folder): if folder_name.startswith("s"): folder_path = os.path.join(masked_folder, folder_name) for file_name in os.listdir(folder_path): image_path = os.path.join(folder_path, file_name) image = imread(image_path) # 使用 skimage.io.imread 读取照片 x_feature = feature.hog(image, orientations=8, pixels_per_cell=(10, 10), cells_per_block=(1, 1), visualize=False, multichannel=True) X_masked.append(x_feature) y_masked.append(1) # 戴口罩类别标签为1 # 将特征和标签转换为NumPy数组 X = np.concatenate([X_unmasked, X_masked]) y = np.concatenate([y_unmasked, y_masked]) # 分割训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 创建并拟合支持向量机模型 svm_model = SVC() svm_model.fit(X_train, y_train) # 在测试集上进行预测 y_pred = svm_model.predict(X_test) # 计算准确率 accuracy = accuracy_score(y_test, y_pred) # 输出准确率 print("准确率:", accuracy) ``` 在修改后的代码中,我使用了 `skimage.io.imread` 函数来读取照片,并在调用 `feature.hog` 函数时,设置了 `multichannel=True` 参数来指示图像是多通道的。这样可以确保 `feature.hog` 函数正确处理多通道图像。 请注意,你可能需要在运行代码之前确保已安装所需的库。你可以使用 `pip` 命令来安装缺少的库,例如 `pip install scikit-image`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是土豆大叔啊!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值