這似乎是「已經回答」的話題的次數太多了,但我仍然無法找到一個可行的解決方案。我需要用JavaCV序列化IplImages和Mats。我不能使用文件系統,我必須堅持使用JavaCV 1.2/JavaCPP 1.2.4/OpenCV 3.1(注意:我不能使用OpenCV自己的Java包裝 - 我必須使用JavaCV)。我在Stackoverflow上發現了許多建議,但它們都是:1)使用不推薦的方法,或2)使用不再存在的方法。我知道IplImages和Mats很容易互換,所以一個解決方案適用於另一個。理想的解決方案將是一種將IplImage/Mat轉換爲字節數組並返回的方法。我希望你們能幫忙。IplImage/Mat到字節數組,回到JavaCV 1.2
0
A
回答
0
我有最新的JavaCV解決方案。實際上,有幾個。我遇到了一些圖像的麻煩,所以我第二次嘗試轉換爲字節數組產生了更一致的結果。 這裏的解決方案在斯卡拉。
要轉換爲包含圖像數據的字節數組,您需要獲取字節。
Mat m = new Mat(iplImage);
ByteBuffer buffer = this.image.asByteBuffer();
Mat m = new Mat(this.image);
int sz = (m.total() * m.channels());
byte[] barr = new byte[sz]();
m.data().get(barr);
轉換爲java.nio.ByteBuffer中,使用從圖像的總大小(我轉換成墊),並獲得數據。我忘記了m.total * m.channels是否返回double,long,int或float。我在斯卡拉使用.toInt。
另一種選擇是使用BufferedImage。我的一些圖像使用JavaCV出現了一些奇怪的現象。
BufferedImage im = new Java2DFrameConverter().convert(new OpenCVFrameConverter.ToIplImage().convert(this.image))
BytearrayOutputstream baos = new ByteArrayOutputStream();
byte[] barr = null;
try{
ImageIO.write(im,"jpg",baos);
baos.flush();
barr = baos.toByteArray();
}finally{
baos.close();
}
//This could be try with resources but the original was in Scala.
要從字節數組轉換爲IplImage,實際上我使用緩衝圖像的可靠性。
ImplImage im = null;
InputStream in = new ByteArrayInputStream(bytes);
try {
Mat m = new Mat(image.getHeight,image.getWidth,image.getType,new BytePointer(ByteBuffer.wrap(image.getRaster.getDataBuffer.asInstanceOf[DataBufferByte].getData)));
im = new IplImage(m);
}finally{
in.close();
}
//Again this could be try with resources but the original example was in Scala
相關問題
- 1. 轉換字節數組中JavaCV
- 2. 字節數組到
- 3. 長MAC地址回到字節數組
- 4. 將字節複製到字節數組?
- 5. 將字節插入到字節數組
- 6. 將字節數組轉換回字節數組返回字節數組
- 7. 轉換字節數組圖像並返回到字節數組,值改變
- 8. 位數組到字節 - java
- 9. 流PDF到字節數組
- 10. 字節數組到變體
- 11. 字節數組到NSData
- 12. Java短到字節數組
- 13. 字節數組到UTF8 CString
- 14. C#-Bitmap到字節數組
- 15. 流到字節數組
- 16. ZipEntry到字節數組
- 17. IntPtr到字節[]數組C#
- 18. 文件到字節數組
- 19. int16到字節數組
- 20. 圖像到字節數組
- 21. Unsafe.As從字節數組到ulong數組
- 22. 對象數組到字節數組
- 23. Python字節數組到位數組
- 24. 源字節數組到字符串,然後回到字節數組與源不同
- 25. Android - 從Uri到InputStream到字節數組?
- 26. POJO到字節數組與POJO到json
- 27. 轉到:讀取的字節到數組
- 28. 如何追加no。的字節數組到字節數組?
- 29. 創建從字節數組,得到這樣的字節數組
- 30. 將字節數組串連到字節數組
這正是我一直在尋找的,除了我在Scala上完全不識字。任何人都會打擾在Java中重新編碼? – lcofresi
給我一分鐘。將會有2個。這來自我的回購https://github.com/SimplrTek/GoatImaging2 –
@Icofresi轉換非常簡單。我爲你留下了一些筆記。 JavaCV示例使用Scala,因爲它可以在編譯JVM字節碼時與Java庫進行交互。 –