为什么我在使用灰度图像的imread时得到参数必须是二维数组的错误?

1 人不认可

我在 feature.local_binary_pattern 中得到一个错误。

I used imread(img_path, cv2.COLOR_BGR2GRAY) but it didn't fix it.

我读到的图片是 grayScale

完整的错误信息。

Traceback (most recent call last):
  File "/Users/myname/PycharmProjects/pythonProject1/main.py", line 76, in <module>
    lbp.append(feature.local_binary_pattern(img, 8, 3, method="default"))
  File "/Users/myname/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/skimage/feature/texture.py", line 333, in local_binary_pattern
    check_nD(image, 2)
  File "/Users/myname/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/skimage/_shared/utils.py", line 655, in check_nD
    raise ValueError(
ValueError: The parameter `image` must be a 2-dimensional array
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
import random
from skimage import feature #-> python -m pip install -U scikit-image
from PIL import  ImageOps
#load and labeling the data
#__________________________________________________________________________
DIRECTORY ="/Users/myname/Desktop/LSB"
FILES = ['cover', 'stego']
data = []
for file in FILES:
    path = os.path.join(DIRECTORY, file)
    for img in os.listdir(path):
        img_path = os.path.join(path, img)
        #print(img_path)
        label = FILES.index(file)
        img = cv2.imread(img_path, cv2.COLOR_BGR2GRAY)
        data.append([img, label])
random.shuffle(data)
for features, label in data:
    X.append(features)
    y.append(label)
X = np.array(X)
y = np.array(y)
 #print(" x[3].shape->" , np.shape(X[3])) ->-> gives me  : x[3].shape-> ()
#LBP feature extraction
#__________________________________________________________________________
lbp =[]
for img in X :  
    print( "**** image shape -> ", np.shape(img) #print only first two images 
    lbp.append(feature.local_binary_pattern(img, 8, 3, method="default"))
    
5 个评论
始终将完整的错误信息(从 "回溯 "一词开始)作为文本放在问题中(而不是评论中)(不是截图,也不是链接到外部门户)。在完整的错误/回溯中还有其他有用的信息。
也许先看看你的图像是什么形状的 - 【替换代码0
@furas 谢谢你,我编辑了问题,加入了完整的错误信息
@furas it's ****-> (512, 512)
i used -> print( "**** image shape -> ", np.shape(X[0])) inside the loop but it's print the shape of the first two image . the third image gives me () . i don't why it's empty
python
arrays
multidimensional-array
parameters
2d
roro6677
roro6677
发布于 2022-09-07
2 个回答
roro6677
roro6677
发布于 2022-09-07
已采纳
0 人赞同

Problem is fixed

问题是由于数组中的无值,由于某种原因,它从空文件中读取,所以我使用if,它不会读取空文件。

for img in os.listdir(path):
        if img!='.DS_Store':
            img_path = os.path.join(path, img)
            print("img path ",  " ", img)
            label = FILES.index(file)
            img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
            data.append([img, label])

在此输入图片描述

在此输入图片描述

furas
furas
发布于 2022-09-07
0 人赞同

当我用图像 RGB 运行你的代码时,我得到 (512,512,3) 。- 即使它在 imread() 中使用 cv2.COLOR_BGR2GRAY 。- 而这可能会导致你的问题。

你在 imread() 中使用了错误的值--它必须是 cv2.IMREAD_GRAYSCALE

img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)  

而数值cv2.COLOR_BGR2GRAY是针对函数的

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

EDIT:

看来你有另一个问题。

cv2无法读取文件时,它不会引发错误,但会返回None。- 之后就会出现问题。你将不得不使用if/else来跳过这个图像。

if img is not None:  # can't be `if not img:`