Convert Bitmap to Mat

Asked

Viewed 73 times

1

Hello, I am trying to grab saved BLOBS in a Sqlite database and convert them to Bitmap. After that, I need to convert these Bitmaps into Mat, to use with Opencv. The problem is that when I open the camera (when these photos should be loaded), the app crasha. Follow the Mainactivity section where I do the conversions:

public void onCameraViewStarted(int width, int height) {
        Log.i(TAG, "Started.");
        //Inicializa Variaveis para personagens
        //Starts Characters Variables
        homer = new Mat();
        homer2 = new Mat();
        Mat homer3 = new Mat();
        marge = new Mat();
        Mat marge2 = new Mat();
        Mat ottoMann1 = new Mat();
        Mat ottoMann2 = new Mat();

        preLoadedImages = new ArrayList<Mat>();

        //Variaveis Temporarias
        //Temp Variables
        Mat rawHomer1 = new Mat();
        Mat rawHomer2 = new Mat();
        Mat rawHomer3 = new Mat();
        Mat rawMarge1 = new Mat();
        Mat rawMarge2 = new Mat();
        Mat rawOttoMann1 = new Mat();
        Mat rawOttoMann2 = new Mat();


        try {

            Cursor c = help.getAll();
            if (c.getCount() > 0) {
            // move to the first result.
            c.moveToFirst();
            //iterate over the results.
            // iterate over rows
                for (int i = 0; i < c.getCount(); i++) {
                byte[] image = c.getBlob(0);
                Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
                Utils.bitmapToMat(bmp, rawHomer1);
                Imgproc.cvtColor(rawHomer1, homer, Imgproc.COLOR_RGB2BGR);
                preLoadedImages.add(homer);//0
                c.moveToNext();

                byte[] image2 = c.getBlob(0);
                Bitmap bmp2 = BitmapFactory.decodeByteArray(image2, 0, image2.length);
                Utils.bitmapToMat(bmp2, rawHomer2);
                Imgproc.cvtColor(rawHomer2, homer2, Imgproc.COLOR_RGB2BGR);
                preLoadedImages.add(homer2);//1
                c.moveToNext();

                byte[] image3 = c.getBlob(0);
                Bitmap bmp3 = BitmapFactory.decodeByteArray(image3, 0, image3.length);
                Utils.bitmapToMat(bmp3, rawHomer3);
                Imgproc.cvtColor(rawHomer3, homer3, Imgproc.COLOR_RGB2BGR);
                preLoadedImages.add(homer3);//2
                c.moveToNext();
            }
            c.close();
        }
            rawMarge1 = Utils.loadResource(this, R.drawable.marge_1);
            rawMarge2 = Utils.loadResource(this, R.drawable.marge_2);
            Imgproc.cvtColor(rawMarge1, marge, Imgproc.COLOR_RGB2BGR);
            Imgproc.cvtColor(rawMarge2, marge2, Imgproc.COLOR_RGB2BGR);
            preLoadedImages.add(marge);//3
            preLoadedImages.add(marge2);//4

            //Otto Man
            rawOttoMann1 = Utils.loadResource(this, R.drawable.otto_1);
            rawOttoMann2 = Utils.loadResource(this, R.drawable.otto_2);
            Imgproc.cvtColor(rawOttoMann1, ottoMann1, Imgproc.COLOR_RGB2BGR);
            Imgproc.cvtColor(rawOttoMann2, ottoMann2, Imgproc.COLOR_RGB2BGR);
            preLoadedImages.add(ottoMann1);//5
            preLoadedImages.add(ottoMann2);//6

            previousDetector = new SimpsonDetector(preLoadedImages);
            previousDetector.ComputeImages();

            DetectedChars = new boolean[preLoadedImages.size()];
            threadControl = new boolean[preLoadedImages.size()];


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Start of Mainactivity, where I start the Imagehelper class which is where the database is created:

Imagehelper help;



    public MainActivity() {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }

    /**
     * Chamado quando a Activity  criada.
     * Called when activity is created
     * */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);

        help = new Imagehelper(this);

Imagehelper class:

public class Imagehelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "clothes.db";
    private static final int SCHEMA_VERSION = 3;

    public Imagehelper(Context context) {
        super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE Image(_id INTEGER PRIMARY KEY AUTOINCREMENT,imageblob BLOB);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    public void insert(byte[] bytes) {
        ContentValues cv = new ContentValues();

        cv.put("imageblob", bytes);
        Log.e("inserted", "inserted");
        getWritableDatabase().insert("Image", "imageblob", cv);

    }

    public Cursor getAll() {
        return (getReadableDatabase().rawQuery("SELECT imageblob FROM Image", null));
    }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.