在OpenCv(Python)中读取文件夹中的多个图像

19
我希望使用opencv (python)从同一个文件夹中读取多个图像。为此,我需要使用for循环或while循环以及imread函数吗?如果需要,如何操作?请帮助我...
我想将图像加载到数组中,并通过循环逐个处理它们。

可能是[Python 2.7打开非默认目录中的多个文件(用于opencv)]的重复问题(http://stackoverflow.com/questions/22312328/python-2-7-opening-multiple-files-from-non-default-directory-for-opencv) - Miki
8个回答

43
import glob
import cv2

images = [cv2.imread(file) for file in glob.glob("path/to/files/*.png")]

我正在尝试使用以下代码打开和保存一个文件夹中的TIFF图像,并希望将它们粘贴到另一个文件夹中,但是输出文件没有生成。import cv2 images = [cv2.imread(file) for file in glob.glob("C:/Users/jitesh.vachheta/Desktop/Dataset/Output_diretory/Test/*.Tiff")] for i in images: cv2.imwrite('my output directory%i,i) - %i placeholder for unique name - Jitesh Vacheta
这只返回空列表。 - Amir Charkhi

14

这将获取文件夹中的所有文件到onlyfiles。然后它将读取它们并将它们存储在数组images中。

from os import listdir
from os.path import isfile, join
import numpy
import cv2

mypath='/path/to/folder'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = numpy.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
  images[n] = cv2.imread( join(mypath,onlyfiles[n]) )

5
import glob
import cv2 as cv

path = glob.glob("/path/to/folder/*.jpg")
cv_img = []
for img in path:
    n = cv.imread(img)
    cv_img.append(n)

4

这个更加高效。

def read_img(img_list, img):
    n = cv2.imread(img, 0)
    img_list.append(n)
    return img_list

path = glob.glob("*.bmp") #or jpg
list_ = []`

cv_image = [read_img(list_, img) for img in path]


1

这是我如何在不使用glob的情况下完成它,而是使用os模块,因为我无法在我的计算机上使用glob来使其工作:

# This is to get the names of all the files in the desired directory
# Here I assume that they are all images
original_images = os.listdir('./path/containing/images')

# Here I construct a list of relative path strings for each image
original_images = [f"./path/containing/images/{file_name}" for file_name in original_images]

original_images = [cv2.imread(file) for file in original_images]

1
import cv2
from pathlib import Path

path=Path(".")

path=path.glob("*.jpg")

images=[]`


for imagepath in path.glob("*.jpg"):

        img=cv2.imread(str(imagepath))
        img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)                         
        img=cv2.resize(img,(200,200))
        images.append(img)
print(images)

0
def flatten_images(folder):               # Path of folder (dataset)
    images=[]                             # list contatining  all images
    for filename in os.listdir(folder):
        print(filename)
        img=plt.imread(folder+filename)  # reading image (Folder path and image name )
        img=np.array(img)                #
        img=img.flatten()                # Flatten image 
        images.append(img)               # Appending all images in 'images' list 
    return(images)

-1
你可以试试这个。
import glob
import cv2


path="D:\OMR_IMAGES\*.jpg"    #Replace with your folder 
path("your path\*.jpg") 
k=glob.glob(path)
images=[cv2.imread(images) for images in glob.glob(path)]

print(len(images))     #number of images in folder
for i in range(len(images)):
    cv2.imshow("images",images[i])
    cv2.waitKey(0)

在我的情况下它起作用了


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接