Python 如何从列表中读取图像
嗨,我有一组图像存储为列表项,我想一个接一个地读取图像并对其执行一些操作。我不知道如何遍历列表中的每个图像项。我可以使用Python 如何从列表中读取图像,python,list,numpy,opencv,image-processing,Python,List,Numpy,Opencv,Image Processing,嗨,我有一组图像存储为列表项,我想一个接一个地读取图像并对其执行一些操作。我不知道如何遍历列表中的每个图像项。我可以使用cv2.imread从文件夹中显式地读取它们,但我想使用存储它们的列表元素 我试图读取已存储在列表元素“align”中的对齐图像。我用于图像对齐的子程序如下: def stackImagesECC(file_list): M = np.eye(3, 3, dtype=np.float32) first_image = None stacked_imag
cv2.imread
从文件夹中显式地读取它们,但我想使用存储它们的列表元素
我试图读取已存储在列表元素“align”中的对齐图像。我用于图像对齐的子程序如下:
def stackImagesECC(file_list):
M = np.eye(3, 3, dtype=np.float32)
first_image = None
stacked_image = None
align = []
for file in file_list:
image = cv2.imread(file,1).astype(np.float32) / 255
print(file)
if first_image is None:
# convert to gray scale floating point image
first_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
stacked_image = image
else:
# Estimate perspective transform
s, M = cv2.findTransformECC(cv2.cvtColor(image,cv2.COLOR_BGR2GRAY), first_image, M, cv2.MOTION_HOMOGRAPHY)
w, h, _ = image.shape
# Align image to first image
image = cv2.warpPerspective(image, M, (h, w))
align.append(image)
stacked_image += image
# cv2.imwrite("aligned{}/aligned{}.png".format(file), image)
cv2.imshow("aligned", image)
# cv2.imwrite("output/aligned/",image)
cv2.waitKey(0)
stacked_image /= len(file_list)
stacked_image = (stacked_image*255).astype(np.uint8)
return align
然后我调用了这个函数,使用:
align = stackImagesECC(glob.glob(path))
现在,为了执行一些函数,我尝试从align变量读取这些文件
#function to detect edges in images
def auto_canny(image, sigma=0.33):
# Compute the median of the single channel pixel intensities
img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
v = np.median(image)
# Apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
return cv2.Canny(image, lower, upper)
这是边缘检测子程序,我想读取对齐的图像
for file in range(0,len(align)):
img = cv2.imread(file)
有人能告诉我我做错了什么吗?提前谢谢
list
本身就是一个迭代器,只需对其进行迭代即可
for file in align:
# code here...
align
已经是图像列表。您只需对其进行迭代即可获得所需的图像:
for image in align:
# Do something with the image
for i in range(0, len(align)):
image = align[i] # Get the ith image
# Do something with it
但是,由于您使用的是范围
迭代器,因此您可以直接索引到对齐
,以获得所需内容:
for image in align:
# Do something with the image
for i in range(0, len(align)):
image = align[i] # Get the ith image
# Do something with it
因为您将返回对齐图像的列表,所以此函数中有些内容不再需要。特别是,您不需要计算堆叠图像。您可能也不需要在每次迭代时显示图像
因此:
def stackImagesECC(file_list):
M = np.eye(3, 3, dtype=np.float32)
first_image = None
align = []
for file in file_list:
image = cv2.imread(file,1).astype(np.float32) / 255
if first_image is None:
# convert to gray scale floating point image
first_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
else:
# Estimate perspective transform
s, M = cv2.findTransformECC(cv2.cvtColor(image,cv2.COLOR_BGR2GRAY), first_image, M, cv2.MOTION_HOMOGRAPHY)
w, h, _ = image.shape
# Align image to first image
image = cv2.warpPerspective(image, M, (h, w))
align.append(image)
return align
如果您查看
align
的性质,它已经是一个图像列表,而不是文件名列表。在实际图像本身上使用cv2.imread
将导致错误,因为它需要字符串。@rayryeng感谢您在align:中对文件使用进行了尝试。当我尝试使用canny边缘检测查找边缘时,它抛出了一个错误错误:(-215)\u src.depth()=函数cv::Canny中的0
。这是什么意思?这与读取图像有关吗?depth==0
表示您的图像不是灰度图像。我需要更多关于你在哪里应用这个的信息。这在您当前的问题中不存在。在删除变量M时,它会抛出一个错误,局部变量“M”在赋值之前被引用
哦,对不起,让我把它放回去。我过早地删除了它。我正在尝试使用这些对齐的图像进行边缘检测,并从边缘检测到的图像中对它们执行按位_和操作