简体   繁体   English

RGB图像无法从ByteArray正确加载:opencv java(封装在scala中)

[英]RGB Image doesn't load correctly from ByteArray: opencv java (wrapped in scala)

I have a camera streaming raw RGB data to the cloud. 我有一台将原始RGB数据流传输到云的相机。 I am picking up that data and processing it in Spark (using scala and OpenCV Java). 我正在拾取数据并在Spark中进行处理(使用scala和OpenCV Java)。 I am having a problem loading correctly the RGB. 我在正确加载RGB时遇到问题。 This is the original image: 这是原始图像:

在此处输入图片说明

And this is what I am getting instead: 这就是我得到的:

在此处输入图片说明

The code I am using for this is: 我为此使用的代码是:

//mainCam is a byte array: val mainCam: Array[Byte]

val mainCam_mat = new Mat (360, 480, CvType.CV_8UC3)
mainCam_mat.put(0,0,mainCam)

//Destination is the path and file name e.g. /tmp/test/123.jpeg
Highgui.imwrite(destination, mainCam_mat)

Based on the input below I tried forcing the little endiann and using intbuffers this way: 根据下面的输入,我尝试强制使用little endiann并以这种方式使用intbuffers:

//mainCam is a byte array: val mainCam: Array[Byte]
val bb = IntBuffer.allocate(mainCam.length/4)
bb.put(ByteBuffer.wrap(mainCam).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer()) 
mainCam_raw.put(0,0,bb.array.map(_.toByte))
//Destination is the path and file name e.g. /tmp/test/123.jpeg
Highgui.imwrite(destination, mainCam_mat)

The result is this: 结果是这样的:

在此处输入图片说明

Thanks Leonardo for suggesting that data was coming as 4 Channel RGBA. 感谢Leonardo提出数据将以4通道RGBA的形式出现。 Updating the code to: 将代码更新为:

//mainCam is a byte array: val mainCam: Array[Byte]
val mainCam_mat = new Mat (360, 480, CvType.CV_8UC4)
mainCam_mat.put(0,0,mainCam)
//Destination is the path and file name e.g. /tmp/test/123.jpeg
Highgui.imwrite(destination, mainCam_mat)

The result is as follows: 结果如下:

在此处输入图片说明

There is still some grain on the image compared to the first visualization. 与第一次可视化相比,图像上仍然有一些颗粒。 Any thoughts on that? 有什么想法吗?

Thanks a lot for all your comments! 非常感谢您的所有评论! They pointed me in the right direction. 他们向我指出了正确的方向。

In the end I went back to the source and found that the configuration was changed. 最后,我回到源头,发现配置已更改。 I was getting 640*480 image with RGB channels. 我正在使用RGB通道获取640 * 480的图像。

This code works: 此代码有效:

//mainCam is a byte array: val mainCam: Array[Byte]
val mainCam_mat = new Mat (480, 640, CvType.CV_8UC3)
mainCam_mat.put(0,0,mainCam)
//Destination is the path and file name e.g. /tmp/test/123.jpeg
Highgui.imwrite(destination, mainCam_mat)

The result: 结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM