我喜欢将Mat类型的数据从OpenCV推到一个csv文件中。我可以从Mat转换为字节数组,然后写入文本文件,但是我从没有得到完整的图像。始终获得图像的一部分。有什么事吗
printtoTextFile(Mat d, File file_g){
Size size = d.size();
byte[] a = new byte[(int) (d.width * d.height)];
d.get(0, 0, a);//I get byte array here for the whole image
FileOutputStream fos_g = null;
OutputStreamWriter ow = null;
BufferedWriter fwriter = null;
try {
fos_g = new FileOutputStream(file_g);
ow = new OutputStreamWriter(fos_g);
fwriter = new BufferedWriter(ow);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (x = 0; x < size.height; x++){
for (y = 0; y < size.width; y++){
fwriter.write(String.valueOf(a[(int) (x * size.width + y)]));
ow.flush();
fwriter.write(",");
ow.flush();
}
fwriter.write("\n");
ow.flush();
//fos_g.flush();
}
fos_g.flush();
try {
fos_g.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
OpenCV矩阵是按行存储的,而不是按列存储的,因此您应该在数组访问中交换x
和y
:
fwriter.write(String.valueOf(a[(int) (y * size.width + x)]));
因此,为了提高效率,还应该交换x
和y
循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句