Ask Your Question

sengsational's profile - activity

2017-05-23 10:19:10 -0600 answered a question Best way to store a Mat object in Android

Although I have not run timings, I suspect that going through formats such as XML or JSON would require more space and computing resources than using primitives.

Below is some code that can be used to save and retrieve MatOfKeyPoint to SQLite database in an Android environment. This is using OpenCV 2.4.11 so SIFT is available.

What you see when you run this application is your test image (which you need to supply and put in the drawable folder) with added keypoints.

The siftTest() method starts by computing keyPoints which is MatOfKeyPoint type. The code saves the underlying data of that object in the database, then reads that data out and creates a new object keyPointsFromDb, the contents of which are applied to the original image and the result is displayed.

public class MainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("opencv_java");
        System.loadLibrary("nonfree");

    }
    private ImageView imageView;
    private Bitmap inputImage; // make bitmap from image resource
    private FeatureDetector detector = FeatureDetector.create(FeatureDetector.SIFT);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inputImage = BitmapFactory.decodeResource(getResources(), R.drawable.test);
        imageView = (ImageView) this.findViewById(R.id.imageView);
        siftTest();
    }

    public void siftTest() {
        Mat rgba = new Mat();
        Utils.bitmapToMat(inputImage, rgba);
        MatOfKeyPoint keyPoints = new MatOfKeyPoint();
        Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGBA2GRAY);
        detector.detect(rgba, keyPoints);

        // Save to database
        MatchingDatabaseAdapter.addKeypoints(keyPoints, getApplicationContext());

        // Opens database cursor
        MatchingDatabaseAdapter.getAllRecordsCursor(getApplicationContext()); 

        // Gets the first item in the database (as an example... you could loop through many/all)
        MatOfKeyPoint keyPointsFromDb = MatchingDatabaseAdapter.getKeypointsFromNextCursorPosition();

        // Closes database
        MatchingDatabaseAdapter.closeDb(); 

        Features2d.drawKeypoints(rgba, keyPointsFromDb, rgba);
        Utils.matToBitmap(rgba, inputImage);
        imageView.setImageBitmap(inputImage);

    }
}

Here is the database code which contains some details associated with converting to the byte array required for the database. I didn't include everything associated with using a database, since that's really a different topic.

public class MatchingDatabaseAdapter {
    ...
    ...
    ...

    public static void addKeypoints(MatOfKeyPoint keyPoints, Context context) {
        float[] data = new float[(int)keyPoints.total() * keyPoints.channels()]; // make a spot to save the data
        keyPoints.get(0,0,data); // load the data;
        ByteBuffer buffer = ByteBuffer.allocate(data.length * 4);
        for (int i = 0; i < data.length; i++){
            buffer.putFloat(data[i]);
        }
        byte[] byteArray = buffer.array();
        addBlob(byteArray, keyPoints.rows(), keyPoints.cols(), keyPoints.type(), context);
    }

    public static void addBlob(byte[] blob, int rows, int columns, int mattype, Context context) throws SQLException {
        if (mDb == null) mDb = openDb(context);
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.BLOB_FIELD_NAME, blob);
        contentValues.put(DatabaseHelper.ROWS_FIELD_NAME, rows);
        contentValues.put(DatabaseHelper.COLUMNS_FIELD_NAME, columns);
        contentValues.put(DatabaseHelper.MATTYPE_FIELD_NAME, mattype);
        long x = mDb.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
        Log.v(TAG, "insert said " + x + " and blob was " + blob.length + " long.");
        closeDb();
    }

    public static Cursor getAllRecordsCursor(Context context) {
        if (mDb == null || !mDb.isOpen()) mDb = openDb(context);
        mCursor = mDb.query(DatabaseHelper.TABLE_NAME, null, null, null, null,null, null);
        boolean hasRecords = mCursor.moveToFirst();
        Log.v(TAG, "MatchingDatabaseAdapter.getAllRecordsCursor() cursor created. " + mCursor + " and " + (hasRecords?"has records":"has NO RECORDS"));
        return mCursor;
    }

     public static MatOfKeyPoint getKeypointsFromNextCursorPosition() {
        MatOfKeyPoint keyPoints = null;
        if (mCursor != null) {
            Log.v(TAG, "mCursor has ...
(more)
2017-05-20 13:21:15 -0600 answered a question Best way to store a Mat object in Android

Although I have not run timings, I suspect that going through formats such as XML or JSON would require more space and computing resources than Bitmap, which , as you have noted, is easily exchangable with the Mat object. Also, going though JSON requires an additional library.

Given the ability to convert to/from Mat and Bitmap, and also given the ability to convert to/from Bitmap to byte[], you have the ability to save in Android's SQLite database as a blob. The utility class below demonstrates the latter conversion:

public class DbBitmapUtility {

    // convert from bitmap to byte array
    public static byte[] getBytes(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0, stream);
        return stream.toByteArray();
    }

    // convert from byte array to bitmap
    public static Bitmap getImage(byte[] image) {
        return BitmapFactory.decodeByteArray(image, 0, image.length);
    }
}

XML and JSON have their uses, but it would seem that in this case, since we have the ability to store and retrieve a binary without introducing an interstitial format, it might be wise to avoid their use in this case.

SQLite is quite straight forward to use:

public void insert( String name, byte[] imageByteArray) throws SQLiteException{
    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues cv = new  ContentValues();
    cv.put(KEY_NAME,    name);
    cv.put(KEY_IMAGE,   imageByteArray);
    database.insert( DB_TABLE, null, cv );
}

public byte[] getByteArray(Cursor cursor) { 
    return = cursor.getBlob(BLOB_COL_NUM);  
}
2017-05-17 11:16:01 -0600 commented question Is Feature Detection Feasible on Android?

@Bob Woodley, Did you ever find a way to match better?

2017-05-17 11:13:33 -0600 commented question Using SIFT/SURF obj. detection with Android

@panc, Did you get the project working?

2017-05-17 11:10:56 -0600 answered a question how to use opencv in android studio in native c++ code ??

There is a similar question here with code on github.

There is a feature of Android Studio to Include C++ Support which generates an external build file called CMakeLists.txt. The similar question has changes required to that file to get the C++ portion of OpenCV to compile in Android Studio.

2017-05-17 11:10:36 -0600 commented question how to use opencv in android studio in native c++ code ??
2017-05-17 11:08:41 -0600 commented question How to store images in SQlite database with opencv java ?

@Teju1106, Did you have any results with your project?

2017-05-17 11:07:19 -0600 answered a question Android Studio: OpenCV + NDK

There is a similar question here with code on github.

There is a feature of Android Studio to Include C++ Support which generates an external build file called CMakeLists.txt. The similar question has changes required to that file to get the C++ portion of OpenCV to compile in Android Studio.

2017-05-17 11:06:53 -0600 commented question Android Studio: OpenCV + NDK

Possible duplicate question: http://answers.opencv.org/question/99...

2017-05-17 11:02:19 -0600 commented question Template Matching with Multiple Objects for android and java using opencv

@siline, why is the result matrix the difference between the sizes of the two input images?

2017-05-17 10:20:25 -0600 answered a question Trouble setting up OpenCV with NDK in Android Studio

There is a similar question here with code on github.

There is a feature of Android Studio to Include C++ Support which generates an external build file called CMakeLists.txt. The similar question has changes required to that file to get the C++ portion of OpenCV to compile in Android Studio.

2017-05-17 10:10:09 -0600 received badge  Organizer (source)
2017-05-17 09:50:38 -0600 received badge  Supporter (source)
2016-08-08 06:17:12 -0600 commented question What would be the best detectors for retail packaging on Android?

Attempted to improve the question based on your inquiry. The goal is to match retail packaging in the library with one arriving on the device, so if you can improve the question, please do.

2016-08-07 09:55:39 -0600 asked a question What would be the best detectors for retail packaging on Android?

Given a set of packaging matches that numbered few enough to be completely downloaded to the Android device, what would be a good approach for detection / classification? My first thought was keypoints, since typical retail packaging has plenty of structure to work with. False positives might need attention, perhaps managed with geometric verification. HOG-template with sliding window and multiple scales as primary detection might be too process intensive for the device.

If someone here has experience in this area and knows what works and what should be avoided, that would save a lot of experimentation. Or if there's code somewhere that does something like this that works, that starting point would also be very helpful.

Update May 2017:

After putting this project aside for almost a year, I've decided to try again. Unfortunately there's way more questions here than answers.

2016-07-31 09:08:22 -0600 received badge  Editor (source)
2016-07-30 20:27:34 -0600 asked a question Rough outline for grocery shopping Android app

Given a library of 500 or 1000 pictures of items from a grocery store shelf, and the requirement to detect any of those items using an Android device's camera, what would be some logical ways to construct this kind of detection app?

Although new pictures would be getting added every so-often, let's presume that the set of pictures in the library would be fixed. Thus would the data resulting from training reside on the Android device, and not depend upon support from a web site?

Neither the library pictures, nor the device user's aim will be perfect, but we can presume both are upright within a few degrees, and positioned generally in front of the object. So a rectangular object should appear roughly rectangular and roughly upright in the library and as the user positions the camera. The library image would only include only the product packaging (nothing surrounding it). The majority of the user's image will contain the product packaging. In other words, the requirement is to have the user frame a single product (although there will be artifacts on the edges).

Are there any open source projects that are similar to what is described in this question? Are there any open source projects (Android or not) that load training data from a set of images into blobs in a database, then compare those blobs to the "current" image?

Would a typical Android device have enough processing power to check video frames, or would the user need to "take a picture" and then let the app churn to get a result?

2016-03-23 03:15:38 -0600 received badge  Student (source)
2016-03-23 01:05:44 -0600 asked a question How can I update the documentation

On this page concerning installation, it currently says:

You can choose downloading ADT Bundle package that in addition to Android SDK Tools includes Eclipse + ADT + NDK/CDT plugins, Android Platform-tools, the latest Android platform and the latest Android system image for the emulator - this is the best choice for those who is setting up Android development environment the first time!

But this is no longer a supported development environment, as indicated here.

How do required changes to the OpenCV documentation get in the queue?