1

**

  • Answer: when working with openCv always use relative path.

**

When I try and read a txt file that is in the same directory as my png file I'm successful

Hey, I'm trying to read an image using OpenCv in my Java app, I'm using java 17(just updated today from java 8) and openCv 453. In the picture you could see the path for the image named src and image named mask and the returned Mat I get from the function imread of imageCodecs.

When I use the File.exists method on my path it returns false. My path Is correct I just copy and paste it from the folder where the file is, how can I get a more accurate path?

Actual code:

public class main
{
    public static void main(String[] args)
    {
        try
        {
        

String path = "C:\\Users\\theit\\OneDrive\\שולחן העבודה\\Hole_Filling_Lightricks\\Lenna.png";
        String mask = "C:\\Users\\theit\\OneDrive\\שולחן העבודה\\Hole_Filling_Lightricks\\Mask.png";
            int z = Integer.parseInt(args[2]);
            float eps = Float.parseFloat(args[3]);
            int connectivity = Integer.parseInt(args[4]);

            if(connectivity != 4 && connectivity != 8)
                throw new Exception("Connectivity type is not 4 or 8");

            System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

            Imgcodecs imageCodecs = new Imgcodecs();
            Mat src = imageCodecs.imread(path, Imgcodecs.IMREAD_GRAYSCALE);
            int rows = src.rows();
            int cols = src.cols();
            Mat imageMask = imageCodecs.imread(mask);

        }

        catch (Exception e)
        {
            System.out.println(e.getMessage());
            System.exit(1);
        }
    }
}

path = C:\Users\theit\OneDrive\שולחןהעבודה\Hole_Filling_Lightricks\Lenna.png

CurrentWorkingPath = C:\Users\theit\OneDrive\שולחן העבודה\Hole_Filling_Lightricks

How can I solve this problem?

Itamarled
  • 145
  • 11
  • as always, check that the file can be found and accessed. use absolute paths if you don't know how relative paths are resolved. – Christoph Rackwitz Oct 07 '21 at 17:55
  • When I use the File.exists method on my path it returns false. My path Is correct I just copy and paste it from the folder where the file is, how can I get a more accurate path? – Itamarled Oct 07 '21 at 18:24
  • Your path is clearly wrong if it doesn't exist. Please show actual code and actual errors rather than pictures of them! Also, help yourself in your code and print the path before you use it, along with your current working directory. Thank you. – Mark Setchell Oct 07 '21 at 18:46
  • @MarkSetchell Added to the main post what you asked about – Itamarled Oct 07 '21 at 18:59
  • show us exactly how you call your program. try putting those paths into the code itself, as literal strings. remember to escape the backslashes. – Christoph Rackwitz Oct 07 '21 at 19:15
  • oh... you should print `args[0]` because that is *not* an argument, that's the executable itself – Christoph Rackwitz Oct 07 '21 at 19:18
  • @ChristophRackwitz A print of args[0]: C:\Users\theit\OneDrive\שולחןהעבודה\Hole_Filling_Lightricks\Lenna.png Edited the main post with the litral string as you asked – Itamarled Oct 07 '21 at 19:22
  • oookay now that's interesting. I admit, I'm not practiced with java. `Files.exists()` returning false should allow you to investigate further... check the existence of every parent directory. see which of those work and which don't. – Christoph Rackwitz Oct 07 '21 at 19:27
  • @ChristophRackwitz The interesting part is that I can read a txt file from this exect dir with no problem so I don't think there is an actual problem with any parent dir – Itamarled Oct 07 '21 at 19:27
  • I am guessing that OpenCV's string handling has issues with *unicode*. I remember other people having issues like that... I think you'll need to use a different directory then, or hope that relative paths avoid this issue (and make sure to use paths relative to the current working directory) – Christoph Rackwitz Oct 07 '21 at 19:30
  • 1
    Does this answer your question? [How do I read an image from a path with Unicode characters?](https://stackoverflow.com/questions/43185605/how-do-i-read-an-image-from-a-path-with-unicode-characters) the approaches may use python, but they can be translated to java – Christoph Rackwitz Oct 07 '21 at 19:32
  • @ChristophRackwitz The answer was using a relative path, thank you very much for your help! – Itamarled Oct 07 '21 at 19:52

0 Answers0