Python初学者:需要目录帮助
我想在这里做的是创建一个“image_path”列表,其中包含所有.pgm文件的路径(我希望用作面部识别程序的测试图像的3.pgm文件除外)。对于除“3.pgm”之外的图像,我想将它们叠加,为此,我使用了数据库“8.pgm”中的一幅图片作为基础图像,在基础图像上我应用了addWeighted()函数,该函数迭代图像路径列表中的每个路径,并在基础图像上不断添加每个图像。我在for循环中使用resize()来保持图像的大小不变 我使用的是AT&T面部数据库,其格式为.pgm文件。我面临的问题是代码的路径行。在这里,我创建了每个.pgm文件的路径列表Python初学者:需要目录帮助,python,opencv,image-processing,Python,Opencv,Image Processing,我想在这里做的是创建一个“image_path”列表,其中包含所有.pgm文件的路径(我希望用作面部识别程序的测试图像的3.pgm文件除外)。对于除“3.pgm”之外的图像,我想将它们叠加,为此,我使用了数据库“8.pgm”中的一幅图片作为基础图像,在基础图像上我应用了addWeighted()函数,该函数迭代图像路径列表中的每个路径,并在基础图像上不断添加每个图像。我在for循环中使用resize()来保持图像的大小不变 我使用的是AT&T面部数据库,其格式为.pgm文件。我面临的问题是代码的
import cv2
import numpy as np
import os
path=['test/']
#Appends all paths of images in the list image_paths. **I am facing error in this line of code**
image_paths = [os.path.join(path, f) for f in os.listdir(path) if not f.endswith('2.pgm')]
#initializing a base_image over which other images will be superimposed
base_image=cv2.imread('8.jpg')
#resizing the base image so it matches the size of the database pics (113(rows),97(columns))
base_image=cv2.resize(base_image,(97,113))
#cv2.imshow('lol',base_image) //for testing purposes
for pics in image_paths:
im=cv2.imread(os.path.expanduser(pics))
base_image=cv2.addWeighted(base_image,0.5,im,0.5,0)
base_image=cv2.resize(base_image(91,113))
cv2.imshow('compiledimg.jpg',imstack)
k = cv2.waitKey(0) & 0xFF
if k == 27:
cv2.destroyAllWindows()
elif k == ord('s'):
cv2.imwrite('compiledimg.jpg',imstack)
cv2.destroyAllWindows()
我面临的错误是:
image_paths = [os.path.join(path, f) for f in os.listdir(path) if not f.endswith('2.pgm')]
TypeError: coercing to Unicode: need string or buffer, list found
好的,这是一个简单的语法问题 该行:
path=['test/']
将更改为:
path='test/'
谢谢夸姆拉纳。当您定义
path=['test/']
时,python解释器认为您正在定义一个列表。然而,由于您不是,所以需要去掉方括号并将路径设置为字符串。因此,正确的语法应该是path='test/'
,看起来path
只需要是字符串'test/'
,而不是列表。但是,我如何迭代“test/”中的每个.pgm文件,并将其添加到基本映像中呢?我需要一个包含每个.pgm文件绝对路径的列表,然后在该列表上运行一个循环,并将每个文件连续添加到基本映像上。如果我自己不运行代码,我想我建议的更改将创建映像路径
。很抱歉,我不明白你在说什么。我只是说你应该在代码中更改一行:path='test/'
,然后再试一次。