简体   繁体   English

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

[英]OpenCV read multiple images from a directory (java)

I'm trying to read multiple images from a file using OpenCV. 我正在尝试使用OpenCV从文件读取多个图像。 The current code I have can only read one image at a time 我拥有的当前代码一次只能读取一张图像

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

I'd like to load the images into an array and then read them using OpenCV. 我想将图像加载到数组中,然后使用OpenCV读取它们。 I'm not sure how to proceed. 我不确定如何进行。 Any help would be appreciated. 任何帮助,将不胜感激。

The same logic can be translated into Java. 可以将相同的逻辑转换为Java。 Just Google around to see how to open a directory and iterate through its files in Java. 只是Google到处看看如何打开目录并使用Java遍历其文件。 The code and comments should be self-explanatory. 代码和注释应不言自明。

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

EDIT 1 Iterate through folder in Java 编辑1 遍历Java中的文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM