Ask Your Question
0

KNN findNearest() return always 0.0 openCV4Android 3.0.0

asked 2015-08-05 15:24:41 -0600

DanielQ gravatar image

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

edit retag flag offensive close merge delete

Comments

hmm, your trainClasses.put(i, 0, myint); looks weird to me, you're pushing the whole myint array to the 1st,2nd 3rd position ? imho, this should go after the loop like: trainClasses.put(0, 0, myint); also, your labels should be integer(classification) not float.

berak gravatar imageberak ( 2015-08-05 23:59:37 -0600 )edit

can you make an answer(or, if you're not allowed, a comment) saying, what actually fixed your case ?

berak gravatar imageberak ( 2015-08-06 14:33:10 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-08-06 14:29:16 -0600

DanielQ gravatar image

@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

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-08-05 15:24:41 -0600

Seen: 1,286 times

Last updated: Aug 05 '15