【发布时间】:2021-09-15 07:24:58
【问题描述】:
我想使用 for 循环提取我的目录中存在的 2 张照片。在每次迭代中,它都会带来一张照片。获得两张照片后,另一个 if 语句将触发并打印“成功从目录中提取的两张图像”。这是代码 ==>
import cv2
import os
import re
from skimage.io import imread,imshow,imsave
images =os.listdir('D:\programs python/regeneration\Mi3_Aligned/1')
img = None
ref_img = None
for i in images:
if i == "1.bmp":
img = imread('D:\programs python/regeneration\Mi3_Aligned/1/' + i)
img = cv2.resize(img, (980, 980), cv2.INTER_AREA)
if i == "2.bmp":
ref_img = imread('D:\programs python/regeneration\Mi3_Aligned/1/'+ i)
ref_img = cv2.resize(img, (980, 980), cv2.INTER_AREA)
if (img!=None and ref_img!=None):
print("Both images extracted from directory successfully")
但它正在产生错误,我无法理解问题所在
if (img!=None and ref_img!=None):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
请帮忙!!!
【问题讨论】:
-
通常最好使用
is not None而不是!=None。 -
不是直接回答您的问题,而是您为什么将
\和/混合在'D:\programs python/regeneration\Mi3_Aligned/1/'中(为什么您对该路径进行了三次硬编码)? -
错误信息你不清楚怎么办?
-
@khelwood 请将其转化为答案。这是第一个正确答案,也是迄今为止唯一正确的答案。
-
@ChristophRackwitz 你为什么不回答?
标签: python arrays opencv scikit-image