带有OpenCV的Java无法读取PNG图像

带有OpenCV的Java无法读取PNG图像,java,opencv,unicode,png,Java,Opencv,Unicode,Png,已解决&为什么: import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.Highgui; public class Test { public static void main (String []args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat img_object = Highgui.imrea

已解决&为什么

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class Test 
{

public static void main (String []args)
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat img_object = Highgui.imread("E:/ℤIMAGEℂ/ℤtestℂ.png");
    System.out.println(
            "img_object.width() = " + img_object.width() 
            + ",\n img_object.height() = " + img_object.height()
            + ",\n img_object.depth() = " + img_object.depth()
            + ",\n img_object.channels() = " + img_object.channels()
            + ",\n img_object.total() = " + img_object.total()
            + ",\n img_object.type() = " + img_object.type()

    );
    }
}
img_object.width() = 0,
img_object.height() = 0,
img_object.depth() = 0,
img_object.channels() = 1,
img_object.total() = 0,
img_object.type() = 0
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
OpenCV Error: Assertion failed (code) in cv::imencode, file           ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp, line 430
Exception in thread "main" CvException [org.opencv.core.CvException:    cv::Exception: ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp:430: error:    (-215) code in function cv::imencode
]
at org.opencv.highgui.Highgui.imencode_1(Native Method)
at org.opencv.highgui.Highgui.imencode(Highgui.java:243)
at Imshow.imshow(Imshow.java:29)
at test.main(Test.java:21)
图像的路径包含Unicode字符,我不得不说这是一个bug

原创帖子

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class Test 
{

public static void main (String []args)
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat img_object = Highgui.imread("E:/ℤIMAGEℂ/ℤtestℂ.png");
    System.out.println(
            "img_object.width() = " + img_object.width() 
            + ",\n img_object.height() = " + img_object.height()
            + ",\n img_object.depth() = " + img_object.depth()
            + ",\n img_object.channels() = " + img_object.channels()
            + ",\n img_object.total() = " + img_object.total()
            + ",\n img_object.type() = " + img_object.type()

    );
    }
}
img_object.width() = 0,
img_object.height() = 0,
img_object.depth() = 0,
img_object.channels() = 1,
img_object.total() = 0,
img_object.type() = 0
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
OpenCV Error: Assertion failed (code) in cv::imencode, file           ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp, line 430
Exception in thread "main" CvException [org.opencv.core.CvException:    cv::Exception: ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp:430: error:    (-215) code in function cv::imencode
]
at org.opencv.highgui.Highgui.imencode_1(Native Method)
at org.opencv.highgui.Highgui.imencode(Highgui.java:243)
at Imshow.imshow(Imshow.java:29)
at test.main(Test.java:21)
我是OpenCV新手,只需使用java和OpenCV 3.2.0、3.1.0和2.4.3来读取此图像,但没有任何成功,即无法读取宽度或高度,尽管我的目标是查找harris角,而使用其他图像则没有此问题

代码

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class Test 
{

public static void main (String []args)
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat img_object = Highgui.imread("E:/ℤIMAGEℂ/ℤtestℂ.png");
    System.out.println(
            "img_object.width() = " + img_object.width() 
            + ",\n img_object.height() = " + img_object.height()
            + ",\n img_object.depth() = " + img_object.depth()
            + ",\n img_object.channels() = " + img_object.channels()
            + ",\n img_object.total() = " + img_object.total()
            + ",\n img_object.type() = " + img_object.type()

    );
    }
}
img_object.width() = 0,
img_object.height() = 0,
img_object.depth() = 0,
img_object.channels() = 1,
img_object.total() = 0,
img_object.type() = 0
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
OpenCV Error: Assertion failed (code) in cv::imencode, file           ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp, line 430
Exception in thread "main" CvException [org.opencv.core.CvException:    cv::Exception: ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp:430: error:    (-215) code in function cv::imencode
]
at org.opencv.highgui.Highgui.imencode_1(Native Method)
at org.opencv.highgui.Highgui.imencode(Highgui.java:243)
at Imshow.imshow(Imshow.java:29)
at test.main(Test.java:21)
图像

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class Test 
{

public static void main (String []args)
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat img_object = Highgui.imread("E:/ℤIMAGEℂ/ℤtestℂ.png");
    System.out.println(
            "img_object.width() = " + img_object.width() 
            + ",\n img_object.height() = " + img_object.height()
            + ",\n img_object.depth() = " + img_object.depth()
            + ",\n img_object.channels() = " + img_object.channels()
            + ",\n img_object.total() = " + img_object.total()
            + ",\n img_object.type() = " + img_object.type()

    );
    }
}
img_object.width() = 0,
img_object.height() = 0,
img_object.depth() = 0,
img_object.channels() = 1,
img_object.total() = 0,
img_object.type() = 0
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
OpenCV Error: Assertion failed (code) in cv::imencode, file           ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp, line 430
Exception in thread "main" CvException [org.opencv.core.CvException:    cv::Exception: ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp:430: error:    (-215) code in function cv::imencode
]
at org.opencv.highgui.Highgui.imencode_1(Native Method)
at org.opencv.highgui.Highgui.imencode(Highgui.java:243)
at Imshow.imshow(Imshow.java:29)
at test.main(Test.java:21)

错误

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class Test 
{

public static void main (String []args)
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat img_object = Highgui.imread("E:/ℤIMAGEℂ/ℤtestℂ.png");
    System.out.println(
            "img_object.width() = " + img_object.width() 
            + ",\n img_object.height() = " + img_object.height()
            + ",\n img_object.depth() = " + img_object.depth()
            + ",\n img_object.channels() = " + img_object.channels()
            + ",\n img_object.total() = " + img_object.total()
            + ",\n img_object.type() = " + img_object.type()

    );
    }
}
img_object.width() = 0,
img_object.height() = 0,
img_object.depth() = 0,
img_object.channels() = 1,
img_object.total() = 0,
img_object.type() = 0
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
OpenCV Error: Assertion failed (code) in cv::imencode, file           ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp, line 430
Exception in thread "main" CvException [org.opencv.core.CvException:    cv::Exception: ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp:430: error:    (-215) code in function cv::imencode
]
at org.opencv.highgui.Highgui.imencode_1(Native Method)
at org.opencv.highgui.Highgui.imencode(Highgui.java:243)
at Imshow.imshow(Imshow.java:29)
at test.main(Test.java:21)

已解决&为什么

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
public class Test 
{

public static void main (String []args)
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

    Mat img_object = Highgui.imread("E:/ℤIMAGEℂ/ℤtestℂ.png");
    System.out.println(
            "img_object.width() = " + img_object.width() 
            + ",\n img_object.height() = " + img_object.height()
            + ",\n img_object.depth() = " + img_object.depth()
            + ",\n img_object.channels() = " + img_object.channels()
            + ",\n img_object.total() = " + img_object.total()
            + ",\n img_object.type() = " + img_object.type()

    );
    }
}
img_object.width() = 0,
img_object.height() = 0,
img_object.depth() = 0,
img_object.channels() = 1,
img_object.total() = 0,
img_object.type() = 0
libpng warning: Image width is zero in IHDR
libpng warning: Image height is zero in IHDR
libpng error: Invalid IHDR data
OpenCV Error: Assertion failed (code) in cv::imencode, file           ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp, line 430
Exception in thread "main" CvException [org.opencv.core.CvException:    cv::Exception: ..\..\..\..\opencv\modules\highgui\src\loadsave.cpp:430: error:    (-215) code in function cv::imencode
]
at org.opencv.highgui.Highgui.imencode_1(Native Method)
at org.opencv.highgui.Highgui.imencode(Highgui.java:243)
at Imshow.imshow(Imshow.java:29)
at test.main(Test.java:21)
图像路径包含Unicode字符

正如有人建议的那样,如果你解决了你的问题,删除它或回答它

若您尝试从Unicode路径读取图像,则会发生错误,并且我并没有看到相关的解决方案,所以您可以阅读此内容


遗憾的是,OpenCV的Unicode文件路径存在漏洞。这是OpenCV的问题,但我的问题

错误输出似乎与问题中的代码不一致(没有调用
imshow
)。样本中的路径似乎只包含基本拉丁语——这真的再现了问题吗如果你解决了这个问题,要么写在答案中,要么干脆删除这个问题。@DanMašek,我忘了说,原始文件路径如下所示:“E:/ℤ形象ℂ/ℤ测试ℂ.png”,包含许多Unicode字符,我没有意识到Unicode字符会导致错误,所以我想用一个更简单的代码来问这个问题,并修改了“E:/IMAGE/test.png”的路径,但这会导致提问的误导效果。imshow是一个纯图像显示功能,我忘记了删除控制台文本中的信息。我希望听到你对这个代码的结果。如果你解决了这个问题,那么要么自己回答这个问题(如果你认为它可以帮助其他人),要么删除它it@leonbloy好的,我已经发布了一个答案来解释是什么导致了我的问题。