0

我正在尝试使用 OpenCV 从文件中读取多个图像。我目前的代码一次只能读取一张图片

Mat source = Highgui.imread(filename,Highgui.CV_LOAD_IMAGE_COLOR);

我想将图像加载到一个数组中,然后使用 OpenCV 读取它们。我不确定如何进行。任何帮助,将不胜感激。

4

1 回答 1

0

同样的逻辑可以翻译成Java。只需谷歌一下,看看如何打开一个目录并在 Java 中遍历它的文件。代码和注释应该是不言自明的。

def loadImagesFrom(folder):

    for filename in os.listdir(folder): #iterate through the files inside the folder
        print 'FileName', filename

        # At this point, filename is just a string consisting of the file's given title.
        # Simply passing that into OpenCV, will not work because that filename does not exist in the
        # current directory. The file is located in folder/filename. In order to get the exact path,
        # we use os.path.join. The final result will look something like this: /Images/car.png
        # Thereafter we simply feed the path to OpenCV's imread'

        image = cv2.imread(os.path.join(folder, filename))

        # check to see if the image is not empty first.
        # You can do this as well by checking if image.shape.isEmpty() or something along these lines

        if image is not None:
            # Do stuff with your image
        else:
            # Image is empty

编辑 1 遍历 Java 中的文件夹

于 2017-06-28T16:18:03.880 回答