【问题标题】:Convert Mat to Blob and then back to Mat将 Mat 转换为 Blob,然后再转换回 Mat
【发布时间】:2015-01-05 02:06:41
【问题描述】:

基本上我正在尝试使用 OpenCV Android 进行面部识别。 我需要将在面部检测期间通过CvCameraViewFrame 中的inputFrame.gray(); 接收到的Mat 图像转换为byte[] 的blob,以保存到SQLite 数据库中。然后,在识别过程中,将此byte[] 转换回一个Mat 文件,该文件可以在jni 文件夹中的.cpp 文件中使用,因为识别代码是本机的。

【问题讨论】:

  • 所以 sqlite 处理也在 jni/c++ 中?
  • @berak no sqlite 处理在 .java 文件中
  • 啊,好的。那就没办法了。
  • @berak 给我关于您的方案的意见,我将尝试相应地更新我的方案。

标签: android c++ opencv face-recognition mat


【解决方案1】:

[编辑]

事实证明,使用 android 板载方法非常容易:

import org.opencv.core.Mat;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

class SqTable extends SQLiteOpenHelper {
    String table = "mydb";

    public SqlTable(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);     
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+table+" (name TEXT UNIQUE, t INTEGER, w INTEGER, h INTEGER, pix BLOB);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    }
    
    public void dbput(String name, Mat m) {
        long nbytes = m.total() * m.elemSize();
        byte[] bytes = new byte[ (int)nbytes ];
        m.get(0, 0,bytes);
        
        dbput(name, m.type(), m.cols(), m.rows(), bytes); 
    }

    public void dbput(String name, int t, int w, int h, byte[] bytes) {
        Log.d("dbput", name + " " + t + " " + w + "x" + h);
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("t", t); 
        values.put("w", w); 
        values.put("h", h); 
        values.put("pix", bytes);
        db.insert(table, null, values);
        db.close();
    }
    
     public Mat dbget(String name) {
        SQLiteDatabase db = this.getReadableDatabase();
        String [] columns = {"t","w","h","pix"};
        Cursor cursor = db.query(table,columns," name = ?", 
                new String[] { name }, // d. selections args
                null, // e. group by
                null, // f. having
                null, // g. order by
                null); // h. limit
     
        if (cursor != null)
            cursor.moveToFirst();
     
        int t = cursor.getInt(0);
        int w = cursor.getInt(1);
        int h = cursor.getInt(2);
        byte[] p = cursor.getBlob(3);
        Mat m = new Mat(h,w,t);
        m.put(0,0,p);
        Log.d("dbget("+name+")", m.toString());
        return m;
    }
};

// later use in your Activity:
SqlTable sql = new SqlTable(this,"imgs",null,1);
Mat m = new Mat(200,400, CvType.CV_8UC3,new Scalar(0,100,0));
Core.putText(m, "world (~)", new Point(30,80), Core.FONT_HERSHEY_SCRIPT_SIMPLEX, 2.2, new          Scalar(200,200,200));
sql.dbput("hello",m);
// Mat m = sql.dbget("hello");

【讨论】:

  • 你能指导我学习一个 Java 教程,以获得与我刚接触 OpenCV 相同的功能吗?
  • 嗯,我必须像你一样从头开始......但我会看看它。 ;)
  • 如何获取 MAT 的类型,我们可以从 jpeg 中检索它
猜你喜欢
  • 2017-10-16
  • 1970-01-01
  • 2015-11-19
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
相关资源
最近更新 更多