OpenCV read multiple images from a directory (java)

864 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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