简体   繁体   中英

OpenCV read multiple images from a directory (java)

I'm trying to read multiple images from a file using 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. I'm not sure how to proceed. Any help would be appreciated.

The same logic can be translated into Java. Just Google around to see how to open a directory and iterate through its files in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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