Read Multiple images on a folder in OpenCv (python)

前端 未结 6 1313
孤独总比滥情好
孤独总比滥情好 2021-01-31 06:15

I want to read multiple images on a same folder using opencv (python). To do that do I need to use for loop or while loop with imread func

相关标签:
6条回答
  • 2021-01-31 06:21
    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 讨论(0)
  • 2021-01-31 06:27
    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)
    
    0 讨论(0)
  • 2021-01-31 06:29

    This will get all the files in a folder in onlyfiles. And then it will read them all and store them in the array 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]) )
    
    0 讨论(0)
  • 2021-01-31 06:37
    import glob
    import cv2
    
    images = [cv2.imread(file) for file in glob.glob("path/to/files/*.png")]
    
    0 讨论(0)
  • 2021-01-31 06:40

    This one has better time efficiency.

    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]
    
    
    0 讨论(0)
  • 2021-01-31 06:41

    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)
    
    0 讨论(0)
提交回复
热议问题