0

**

  • 答:使用 openCv 时总是使用相对路径。

**

当我尝试读取与我的 png 文件位于同一目录中的 txt 文件时,我成功了

嘿,我正在尝试在我的 Java 应用程序中使用 OpenCv 读取图像,我使用的是 java 17(今天刚刚从 java 8 更新)和 openCv 453。在图片中,您可以看到名为 src 和 image 的图像的路径命名掩码和我从 imageCodecs 的函数 imread 得到的返回 Mat。

当我在路径上使用 File.exists 方法时,它返回 false。我的路径是正确的我只是从文件所在的文件夹中复制并粘贴它,我怎样才能获得更准确的路径?

实际代码:

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);
        }
    }
}

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

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

我怎么解决这个问题?

4

0 回答 0