我正在尝试使用OpenCV Java API创建一个辅助函数来处理输入图像并返回输出字节数组。输入图像是保存在电脑中的jpg文件。输入和输出图像使用Swing在Java UI中显示。将OpenCV Mat对象转换为BufferedImage
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Load image from file
Mat rgba = Highgui.imread(filePath);
Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0);
// Convert back to byte[] and return
byte[] return_buff = new byte[(int) (rgba.total() * rgba.channels())];
rgba.get(0, 0, return_buff);
return return_buff;
当return_buff
被返回并转换为BufferedImage时,我返回NULL。当我注释掉Imgproc.cvtColor
函数时,return_buff
已正确转换为我可以显示的BufferedImage。看起来像Imgproc.cvtColor
正在返回一个我无法在Java中显示的Mat对象。
这里是我的代码从字节[]转换设置为BufferedImage:
InputStream in = new ByteArrayInputStream(inputByteArray);
BufferedImage outputImage = ImageIO.read(in);
在上面的代码中,outputImage是NULL
没有任何人有任何建议或想法?