Ask Your Question

DanielQ's profile - activity

2015-10-15 13:34:23 -0600 commented answer Android saving enormous Mat object (Java)

I convert each image (to train) to binary, and save it to file, but it is 140 MB... Maybe I should use lower size each image (now, I have 200x200 px). PS. I wrote app to hand sign recognioton.

2015-10-12 09:29:51 -0600 asked a question Android saving enormous Mat object (Java)

I'm writting andorid app where I use KNearest. I wrote code to train model, but every restart app, I must train data again, so I would like to save train data - Mat(1000, 40 000, CvType.CV_32FC1); I try to convert this Mat to byte[] and save it to SharedPreferences but I got outOfMemory exception...

Have you any suggestion to speed up or save data? Thx for help.

2015-08-09 12:25:49 -0600 answered a question Saving an image using OpenCV & Android

If you have Mat, you can convert this to Bitmap:

        //subimg -> your frame

        Bitmap bmp = null;
        try {
            bmp = Bitmap.createBitmap(subimg.cols(), subimg.rows(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(subimg, bmp);
        } catch (CvException e) {
            Log.d(TAG, e.getMessage());
        }

        subimg.release();


        FileOutputStream out = null;

        String filename = "frame.png";


        File sd = new File(Environment.getExternalStorageDirectory() + "/frames");
        boolean success = true;
        if (!sd.exists()) {
            success = sd.mkdir();
        }
        if (success) {
            File dest = new File(sd, filename);

            try {
                out = new FileOutputStream(dest);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, out); // bmp is your Bitmap instance
                // PNG is a lossless format, the compression factor (100) is ignored

            } catch (Exception e) {
                e.printStackTrace();
                Log.d(TAG, e.getMessage());
            } finally {
                try {
                    if (out != null) {
                        out.close();
                        Log.d(TAG, "OK!!");
                    }
                } catch (IOException e) {
                    Log.d(TAG, e.getMessage() + "Error");
                    e.printStackTrace();
                }
            }
        }
2015-08-06 15:36:47 -0600 commented question Digit recognition : training responses

I think, k value should be lower than number of classes. So when you put k=32 you have result 0.0

2015-08-06 14:29:16 -0600 commented question KNN findNearest() return always 0.0 openCV4Android 3.0.0

@berak, You right, I put trainClasses.put(0, 0, myint); after the loop and I set k=1 (I tested k > 5, but I had only 5 images....) and it's work. I'm using float (labels), because when I'm using int I have an error: java.lang.UnsupportedOperationException: Mat data type is not compatible: 5 So now, it's work very well. Thx

2015-08-05 15:24:41 -0600 asked a question KNN findNearest() return always 0.0 openCV4Android 3.0.0

I'm writting apk, which recognise hand sign. It's my function to train KNearest model. I'm using 5 images 200x200 px.

   public void train(){
    Mat trainData    = new Mat(0, sizex * sizey, CvType.CV_32FC1); // 0 x 40 000
    Mat trainClasses = new Mat(train_samples, 1, CvType.CV_32FC1); // 5 x 1

    float[] myint = new float[train_samples];
    Mat img = new Mat();

    for (int i = 0; i<train_samples; i++) {

        String photoPath=Environment.getExternalStorageDirectory().toString()+"/frames/frame_"+i+".png";

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
        Utils.bitmapToMat(bitmap, img);

        if (img.channels() == 3) {
            Imgproc.cvtColor(img, img, Imgproc.COLOR_RGB2GRAY);
        } else if (img.channels() == 4) {
            Imgproc.cvtColor(img, img, Imgproc.COLOR_RGBA2GRAY);
        }

        Imgproc.resize(img, img, new Size(sizex, sizey));
        img.convertTo(img, CvType.CV_32FC1);
        img = img.reshape(1, 1); //  1x40 000 ( 200x200 )

        trainData.push_back(img);
        myint[i] = (float) i;
        trainClasses.put(i, 0, myint);
    }

    knn = KNearest.create();
    knn.train(trainData, Ml.ROW_SAMPLE, trainClasses);

    trainClasses.release();
    trainData.release();
}

And my function which test model:

public void test(View view) {
    Mat result = new Mat(1, 1, CvType.CV_8U);

    String photoPath = Environment.getExternalStorageDirectory().toString() + "/frames/frame_3.png";

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
    Utils.bitmapToMat(bitmap, frame_3);

    if (frame_3.channels() == 3) {
        Imgproc.cvtColor(frame_3, frame_3, Imgproc.COLOR_RGB2GRAY);
    } else if (frame_3.channels() == 4) {
        Imgproc.cvtColor(frame_3, frame_3, Imgproc.COLOR_RGBA2GRAY);
    }

    Imgproc.resize(frame_3, frame_3, new Size(sizex, sizey)); // 200x200
    frame_3.convertTo(frame_3, CvType.CV_32FC1);
    frame_3 = frame_3.reshape(1, 1); // 1x40 000  (200x200)
    iv.setImageBitmap(bitmap);

    float f = knn.findNearest(frame_3, 1, result);

    Log.d(TAG, "KNN: " + f);
    result.release();
}

variable f is always 0.0, although I pass the same frame_3 (image in png) in train model and findnearest. I have no idea what I am doing wrong

2015-08-05 09:54:12 -0600 received badge  Critic (source)
2015-08-05 09:01:23 -0600 commented answer How to use KNearest and ANN in OpenCV 3.0

@berak, I check KNearest.java file and actually isn't there save function, but when I write knn. CTRL+Space I have prompt with save function... But it isn't work

2015-08-05 07:10:23 -0600 commented answer How to use KNearest and ANN in OpenCV 3.0

In Android version knn.save("filename") isn't work, apk crash...

2015-07-23 13:48:13 -0600 received badge  Enthusiast
2015-07-20 16:14:28 -0600 received badge  Supporter (source)
2015-07-18 11:35:54 -0600 asked a question openCV4Android v.3.0.0 hand sign recognition in Java

I would like to write an application on Android, which recognise a sign, in my case alphabet. I have done detection a hand (background substraction), with find biggest contours, convex hull and convexity defects. image description

I was reading about K-Nearest Neighborhood, but how to train date? In Android version of openCV I found method findNearest(Mat samples, int k, Mat results, Mat neighborResponses, Mat dist), but I have no idea how to use that. Anybody help me?