![](/img/trans.png)
[英]OpenCV - numpy array is 'bad argument type' for cv2.imread?
[英]Different length of array when cv2.imread receives cv2.IMREAD_GRAYSCALE as argument
Training_Data=[]
IMG_SIZE=100
def build():
Directory="D:\projects\Machine_learning\Dog_Cat\PetImages"
CATEGORY=["Cat","Dog"]
for category in CATEGORY:
path=os.path.join(Directory,category)
class_num=CATEGORIES.index(category)
for img in tqdm(os.listdir(path)):
try:
img_array=cv2.imread(
os.path.join(path,img),
cv2.IMREAD_GRAYSCALE
)
new_array=cv2.resize(img_array(IMG_SIZE,IMG_SIZE))
Training_Data.append([new_array,class_num])
except Exception as e:
pass
當我不傳遞cv2.IMREAD_GRAYSCALE時,它將給出不同長度的數組:
img_array=cv2.imread(os.path.join(path,img))
X = 74598
Y = 24886
為什么不使用cv2.IMREAD_GRAYSCALE
時,在training_data
追加3倍的元素?
默認情況下,當您不傳遞cv2.IMREAD_GRAYSCALE
作為參數時, cv2.imread
將使用三個通道讀取圖像(它將圖像加載為RGB)。 當傳遞cv2.IMREAD_GRAYSCALE
作為參數時,每像素的位數正在變化(每像素只需要1 * 8位,而不是3 * 8)。
如果需要,可以使用depth()
和channels()
函數檢查圖像深度和通道 ,這可能有助於您更好地了解正在發生的情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.