无法处理cvExtractSURF

时间:2012-09-23 12:28:43

标签: android opencv javacv surf

我正在尝试创建一个Android应用程序,它使用JavaCV通过其SURF功能匹配图像。

我正在尝试首先从Assets文件夹加载图像,为其提取SURF功能并将其保存在数据库中。在执行cvExtractSURF函数之前,代码似乎正常工作。实际上,我不确定它是否会被执行。模拟器/调试器开始思考,几分钟后,Eclipse说它已断开连接。真实设备上的同样问题:只是转到Home并且代码永远不会被执行。没有错误记录到Logcat,似乎没有异常抛出,没有.... 以下是发生这种情况的代码。

    package com.landmark;

import java.io.IOException;
import java.io.InputStream;
import java.nio.FloatBuffer;


import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.*;
import android.widget.Toast;
import android.content.Context;
import android.content.res.AssetManager;

import static com.googlecode.javacv.cpp.opencv_core.CvSeq;
import static com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;

import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_features2d.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

public class DatabaseHelper extends SQLiteOpenHelper {
    static final String dbName = "ImageDatabase.db";
    static final String imageTableName = "ImageData";
    static final String colImageID = "ImageID";
    static final String colInfo = "ObjectInformation";
    static final String surfTableName = "SURFData";
    static final String colSURFId = "SURFID";
    static final String colDir = "Dir";
    static final String colHessian = "Hessian";
    static final String colLaplacian = "Laplacian";
    static final String colCoordX = "CoordX";
    static final String colCoordY = "CoordY";
    static final String colSize = "Size";
    static final String colRefDescriptorID = "DescriptorID";
    static final String colRefImageID = "ImageRefID";
    static final String descriptorTableName = "DescriptorData";
    static final String colDescriptorID = "DescriptorRefID";
    static final String colDescriptorValue = "DescriptorValue";

private Context currentContext = null;

public DatabaseHelper (Context context){
    super(context, dbName, null, 1);
    this.currentContext = context;
}

@Override
public void onCreate (SQLiteDatabase database){
    database.execSQL("CREATE TABLE " + imageTableName + 
            " ( " + colImageID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + colInfo + " TEXT );");
    database.execSQL("CREATE TABLE " + surfTableName + 
            " ( " + colSURFId + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + colDir + " REAL, "
            + colHessian + " REAL, "
            + colLaplacian + " INTEGER, "
            + colCoordX + " REAL, "
            + colCoordY + " REAL, "
            + colSize + " INTEGER, "
            + colRefDescriptorID + " INTEGER, " 
            + colRefImageID + " INTEGER, " 
            + " FOREIGN KEY (" + colRefImageID + ") REFERENCES " + imageTableName + " (" + colImageID + "));");
    database.execSQL("CREATE TABLE " + descriptorTableName +
            " ( " + colDescriptorID + " INTEGER, "
            + colDescriptorValue + " REAL, "
            + "FOREIGN KEY (" + colDescriptorID + ") REFERENCES " + surfTableName + " (" + colDescriptorID + "));");



    //image 01
    AssetManager assets = currentContext.getAssets();
    String imageFilename = "HramAlexanderNevski03.jpg";
    InputStream imageStream = null;
    try {
        imageStream = assets.open(imageFilename);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmapImage = BitmapFactory.decodeStream(imageStream);
    int width = bitmapImage.getWidth();
    int height = bitmapImage.getHeight();
    IplImage image = IplImage.create(width, height, IPL_DEPTH_8U, 4);
    IplImage greyImage = IplImage.create(width, height, IPL_DEPTH_8U, 1);
    bitmapImage.copyPixelsToBuffer(image.getByteBuffer());
    cvCvtColor(image, greyImage, CV_BGR2GRAY);
    CvMemStorage storage = CvMemStorage.create();

    CvSeq imageKeypoints = new CvSeq();
    CvSeq imageDescriptors = new CvSeq();
    CvSURFParams params = cvSURFParams(500, 1);

    cvExtractSURF(greyImage, null, imageKeypoints, imageDescriptors, storage, params, 0);
    //storage.release();

    database.execSQL("INSERT INTO "+ imageTableName + " VALUES (" + null + ", " 
            + "'This is the Alexander Nevski memorial'" + ");");
    for(int i=0; i<imageKeypoints.total(); i++){
        CvSURFPoint point =  new CvSURFPoint(cvGetSeqElem(imageKeypoints, i));
        database.execSQL("INSERT INTO " + surfTableName + " VALUES (" + null + ", " + point.dir() + ", "
                + point.hessian() + ", " + point.laplacian() + ", "
                + point.pt().x() + ", " + point.pt().y() + ", "
                + point.size() + ", " + null + ", " + 0 + ");");
        for (int j=0; j<imageDescriptors.total(); j++){
            int size = imageDescriptors.elem_size();
            FloatBuffer descriptors = cvGetSeqElem(imageDescriptors, j).capacity(size).asByteBuffer().asFloatBuffer();
            for (int k=0; k<descriptors.capacity(); k++){
                database.execSQL("INSERT INTO " + descriptorTableName + " VALUES ( (SELECT LAST_INSERT_ROWID() - "
                        + j + "), " + descriptors.get(k) + ");");
            }
        }
    }
}



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

}

public void onOpen(SQLiteDatabase database){
    super.onOpen(database);
}
}

我搜索了类似的问题,但我似乎找不到任何问题。

忘了添加我正在使用OpenCV 2.3.1(由于新JavaCV的一些问题),2012年3月29日的JavaCV,Eclipse Indigo,Windows 7 32位。

我仍然是Android开发的新手,所以一点点的帮助将不胜感激。

0 个答案:

没有答案