android - 将 Mat(OpenCV) 数据类型写入 Java 或 Android 中的 csv 文件

标签 android opencv

我喜欢将 Mat 类型的数据从 OpenCV 转换为 csv 文件。我可以从 垫到字节数组,然后我写入文本文件,但我从来没有得到完整的图像。始终获取图像的一部分。有什么问题吗?

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 矩阵是逐行存储的,而不是逐列存储的,因此您应该交换数组访问中的 xy:

fwriter.write(String.valueOf(a[(int) (y * size.width + x)]));

因此,为了提高效率,您还应该交换 xy 循环。

关于android - 将 Mat(OpenCV) 数据类型写入 Java 或 Android 中的 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22271019/

相关文章:

android - 如何在 Kivy 的 Scrollable GridLayout 中显示图像

opencv - (OpenCV) 从分水岭快速计算邻接矩阵

android - 从 Android 流式传输到 VLC

android - Django 的 x-www-form-urlencoded 解析

python - 类型错误 : mat data type = 0 is not supported

c++ - 使用 openCV 时如何修复链接器错误?

python-3.x - 使用python将图像放在另一个图像上

image-processing - 在肖像图像中检测头发?

android - GCM : how to send paylaod?

java - 安卓工作室 : lots of errors when I run my app