Read Multiple images on a folder in OpenCv (python)

前端 未结 6 1369
孤独总比滥情好
孤独总比滥情好 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: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]) )
    

提交回复
热议问题