由网友(︶﹌彩狼)分享简介:我喜欢从OpenCV的一个CSV文件普林垫类型的数据。我可以从转换垫字节数组,然后我写的文本文件,我从来没有得到完整的图像。总是得到图像的一部分。可能是什么问题? printtoTextFile(马太福音D,文件file_g){ 尺寸尺寸= d.size(); 字节[] A =新的字节[(INT)(为d...
我喜欢从OpenCV的一个CSV文件普林垫类型的数据。我可以从转换垫字节数组,然后我写的文本文件,我从来没有得到完整的图像。总是得到图像的一部分。可能是什么问题?
printtoTextFile(马太福音D,文件file_g){ 尺寸尺寸= d.size(); 字节[] A =新的字节[(INT)(为d.width *为d.height)]; d.get(0,0,一); //我得到字节数组,这里的整体形象 FileOutputStream中fos_g = NULL; OutputStreamWriter流量= NULL; BufferedWriter将fwriter = NULL; 尝试{ fos_g =新的FileOutputStream(file_g); OW =新OutputStreamWriter(fos_g); fwriter =新的BufferedWriter(流); }赶上(FileNotFoundException异常五){ // TODO自动生成catch块 e.printStackTrace(); } 为(X = 0; X&下; size.height; X ++){ 对于(Y = 0; Y< size.width; Y ++){ fwriter.write(将String.valueOf(一[(INT)(X * size.width + Y)])); ow.flush(); fwriter.write(,); ow.flush(); } fwriter.write( n); ow.flush(); //fos_g.flush(); } fos_g.flush(); 尝试{ fos_g.close(); }赶上(IOException异常五){ // TODO自动生成catch块 e.printStackTrace(); } }}
解决方案
OpenCV的矩阵按行,不是逐列存储行,因此你应该交换 X
和是
中的数组访问:
fwriter.write(将String.valueOf(一[(INT)(Y * size.width + X)]));
因此,为了提高效率,你也应该换你的 X
和是
循环。
I like to pring Mat type data from OpenCV to a csv file. I can convert from Mat to byte array, then I write to text file and I never get full image. Always get a portion of the image. What could be wrong?
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 matrices are stored row by row, not column by column, hence you should swap the x
and y
in the array access:
fwriter.write(String.valueOf(a[(int) (y * size.width + x)]));
As a consequence, for efficiency, you should also swap your x
and y
loops.
相关推荐
最新文章