0

I need some help. I created a function that read a single image. Well, it's work, but I want to create something like a loop for get all images from directory and use the imread method for get pixels values. How I can do this? follow my code below.

 public void cor() {
        String src = ("path_to_folder");
        Mat imgread;
        imgread = Imgcodecs.imread(src, IMREAD_COLOR);


        Mat rgbimage = null; //for conversion bgr2rgb

        int lin = imgread.rows(); //get the number of rows
        int col = imgread.cols(); //get the number of cols

        if (imgread.empty()) {
            Log.e("error", "is empty!");
        } else {
            rgbimage = new Mat(imgread.size(), imgread.type());
            Imgproc.cvtColor(imgread, rgbimage, Imgproc.COLOR_BGR2RGB);
        }

        for (int i = 0; i < lin; i++) {
            for (int j = 0; j < col; j++) {
                double[] rgb =rgbimage.get(i, j);
                pixels.add(rgb); //put data in arraylist

            }
        }
}
BDamsk
  • 1
  • 2

1 Answers1

0

Using File you can get a list of all files in a directory. Then, you can loop through the list to get the absolute path of each file and do whatever you want with it.

public void cor() {
    File rootDir= new File("your/path/to/root_directory");
    File[] files = rootDir.listFiles();

    for(File file :files) {
        String src = file.getAbsolutePath();
        Mat imgread;
        imgread = Imgcodecs.imread(src, IMREAD_COLOR);

        /*
         * Do the other stuff in your method. 
         */ 
    }
}

Note: I was not 100% sure what you were doing with pixels, so I just wrote what you need to loop through a directory.

  • "root_directory" is the path for my folder? – BDamsk Apr 26 '18 at 20:18
  • Yes. The "root_directory" is the folder that contains all of the image files. – Shawn Wonsmos Apr 26 '18 at 20:30
  • Checkout [this post](https://stackoverflow.com/questions/38417848/cannot-resolve-symbol-fileutils-in-codepath-todo-app-tutorial-android) – Shawn Wonsmos Apr 26 '18 at 20:36
  • don't worked. Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'. > java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex – BDamsk Apr 26 '18 at 21:13
  • I have updated my response to not use [FileUtils](https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html). This should work now. – Shawn Wonsmos Apr 26 '18 at 21:32
  • the code above return for me "empty" in the "if" where verify if imgread variable contain a image. I guess that right way is getAbsolutFile(); but, the method imread (method that read a image) just accept the path type String. – BDamsk May 02 '18 at 17:53
  • I solved. Thank you. The problem was my own error in sintaxy. Sorry. Now it works. – BDamsk May 02 '18 at 21:13