Home > database >  why I got parameter must be 2D array error when I used imread with grayscale image?
why I got parameter must be 2D array error when I used imread with grayscale image?

Time:09-07

I got an error in feature.local_binary_pattern

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

The images that I read is grayScale

THE FULL Error message:

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)

X=[]
y=[]

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"))

CodePudding user response:

When I run your code with image RGB then I get (512,512,3) - even if it use cv2.COLOR_BGR2GRAY in imread() - and this can make your problem.

You use wrong value in imread()- it has to be cv2.IMREAD_GRAYSCALE

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

And value cv2.COLOR_BGR2GRAY is for function

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

EDIT:

It seems you have another problem.

When cv2 can't read file then it doesn't raise error but it returns None - and later it makes problem. You will have to use if/else to skip this image

if img is not None:  # can't be `if not img:`
   data.append([img, label])
else:
   print("can't read:", img_path)

You may have to also use some external program to convert this image to other format.

CodePudding user response:

Problem is fixed

the problem was because None values in the array for some reason it read from empty file so i used if which will not read the empty file

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])

enter image description here

enter image description here

  • Related