我喜欢将 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
and y
在数组访问中:
fwriter.write(String.valueOf(a[(int) (y * size.width + x)]));
因此,为了提高效率,您还应该交换您的x
and y
loops.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)