OpenCV从目录中读取多个图像(java)

时间:2017-06-28 15:22:19

标签: java opencv highgui

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

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

我想将图像加载到数组中,然后使用OpenCV读取它们。我不知道该怎么办。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分: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 Iterate through folder in Java