Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Android application to recognize known object and draw its border in real time camera frame.

I'm really new to openCV and I followed this and I dont know if what I did is right but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Core.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem?

Here is my code

package com.example.objectrecognition;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Point;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Scalar;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.KeyPoint;
import org.opencv.imgproc.Imgproc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity implements CvCameraViewListener2 {

    private static final String  TAG = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;

    private Mat              mRgba;
    private Mat              mRef;
    private Mat              mGray;  
    private Mat              mBuff;


    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){

    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
    };


@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.MainActivity);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}


public void onCameraViewStarted(int width, int height) {
     mRgba = new Mat();
     mRef  = new Mat();
     mGray = new Mat(); 
    mBuff = new Mat();  

    try {
        mBuff = Utils.loadResource(this, R.drawable.dartlogo);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }        

public void onCameraViewStopped(){
   if (mRgba != null)
       mRgba.release();
   if (mGray != null)
       mGray.release();
   if (mRef != null)
       mRef.release();
   if (mBuff != null)
       mBuff.release();       
   mRgba = null;
   mGray = null;
   mRef = null;
   mBuff = null;
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();  

    MatOfKeyPoint keypointsRef = new MatOfKeyPoint();
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat extractRef = new Mat();
    Mat extract     = new Mat();
    MatOfDMatch matchs = new MatOfDMatch();

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    MatOfDMatch gm = new MatOfDMatch(); 

    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }

    gm.fromList(good_matches); 

    Features2d.drawMatches(mRef, keypointsRef, mGray,
    keypoints, gm, mRgba, new Scalar(255, 0, 0),
    new Scalar(0, 0, 255), new MatOfByte(), 2);       

    LinkedList<Point> objList = new LinkedList<Point>();
    LinkedList<Point> sceneList = new LinkedList<Point>();

    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);

keypointsRef.release();
keypoints.release();
extractRef.release();
extract.release();
matchs.release();
keypointsRef = null;        
keypoints = null;
extractRef = null;
extract = null;
matchs = null;      

  return mRgba;         
}       
}

Any other documentation or reference will be useful. Thanx in advance.

Android application to recognize known object and draw its border in real time camera frame.

I'm really new to openCV and I followed this and I dont know if what I did is right but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Core.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem?

Here is my code

package com.example.objectrecognition;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Point;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Scalar;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.KeyPoint;
import org.opencv.imgproc.Imgproc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity implements CvCameraViewListener2 {

    private static final String  TAG = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;

    private Mat              mRgba;
    private Mat              mRef;
    private Mat              mGray;  
    private Mat              mBuff;


    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){

    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
    };


@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.MainActivity);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}


public void onCameraViewStarted(int width, int height) {
     mRgba = new Mat();
     mRef  = new Mat();
     mGray = new Mat(); 
    mBuff = new Mat();  

    try {
        mBuff = Utils.loadResource(this, R.drawable.dartlogo);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }        

public void onCameraViewStopped(){
   if (mRgba != null)
       mRgba.release();
   if (mGray != null)
       mGray.release();
   if (mRef != null)
       mRef.release();
   if (mBuff != null)
       mBuff.release();       
   mRgba = null;
   mGray = null;
   mRef = null;
   mBuff = null;
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();  

    MatOfKeyPoint keypointsRef = new MatOfKeyPoint();
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat extractRef = new Mat();
    Mat extract     = new Mat();
    MatOfDMatch matchs = new MatOfDMatch();

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    MatOfDMatch gm = new MatOfDMatch(); 

    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }

    gm.fromList(good_matches); 

    Features2d.drawMatches(mRef, keypointsRef, mGray,
    keypoints, gm, mRgba, new Scalar(255, 0, 0),
    new Scalar(0, 0, 255), new MatOfByte(), 2);       

    LinkedList<Point> objList = new LinkedList<Point>();
    LinkedList<Point> sceneList = new LinkedList<Point>();

    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);

keypointsRef.release();
keypoints.release();
extractRef.release();
extract.release();
matchs.release();
keypointsRef = null;        
keypoints = null;
extractRef = null;
extract = null;
matchs = null;      

  return mRgba;         
}       
}

Any other documentation or reference will be useful. Thanx in advance.

Android application to recognize known object and draw its border in real time camera frame.

I'm really new to openCV I followed this and I dont know if what I did is right wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Core.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem?

Here is my code

package com.example.objectrecognition;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Point;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Scalar;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.KeyPoint;
import org.opencv.imgproc.Imgproc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity implements CvCameraViewListener2 {

    private static final String  TAG = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;

    private Mat              mRgba;
    private Mat              mRef;
    private Mat              mGray;  
    private Mat              mBuff;


    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){

    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
    };


@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.MainActivity);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}


public void onCameraViewStarted(int width, int height) {
     mRgba = new Mat();
     mRef  = new Mat();
     mGray = new Mat(); 
    mBuff = new Mat();  

    try {
        mBuff = Utils.loadResource(this, R.drawable.dartlogo);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }        

public void onCameraViewStopped(){
   if (mRgba != null)
       mRgba.release();
   if (mGray != null)
       mGray.release();
   if (mRef != null)
       mRef.release();
   if (mBuff != null)
       mBuff.release();       
   mRgba = null;
   mGray = null;
   mRef = null;
   mBuff = null;
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();  

    MatOfKeyPoint keypointsRef = new MatOfKeyPoint();
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat extractRef = new Mat();
    Mat extract     = new Mat();
    MatOfDMatch matchs = new MatOfDMatch();

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    MatOfDMatch gm = new MatOfDMatch(); 

    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }

    gm.fromList(good_matches); 

    Features2d.drawMatches(mRef, keypointsRef, mGray,
    keypoints, gm, mRgba, new Scalar(255, 0, 0),
    new Scalar(0, 0, 255), new MatOfByte(), 2);       

    LinkedList<Point> objList = new LinkedList<Point>();
    LinkedList<Point> sceneList = new LinkedList<Point>();

    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);

keypointsRef.release();
keypoints.release();
extractRef.release();
extract.release();
matchs.release();
keypointsRef = null;        
keypoints = null;
extractRef = null;
extract = null;
matchs = null;      

  return mRgba;         
}       
}

Any other documentation or reference will be useful. Thanx in advance.

Android application to recognize known object and draw its border in real time camera frame.

I'm really new to openCV I followed this and I dont know what is wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Core.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem?

Here is my code

package com.example.objectrecognition;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Point;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Scalar;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.KeyPoint;
import org.opencv.imgproc.Imgproc;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity implements CvCameraViewListener2 {

    private static final String  TAG = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;

    private Mat              mRgba;
    private Mat              mRef;
    private Mat              mGray;  
    private Mat              mBuff;


    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){

    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
    };


@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.MainActivity);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}


public void onCameraViewStarted(int width, int height) {
     mRgba = new Mat();
     mRef  = new Mat();
     mGray = new Mat(); 
    mBuff = new Mat();  

    try {
        mBuff = Utils.loadResource(this, R.drawable.dartlogo);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }        

public void onCameraViewStopped(){
   if (mRgba != null)
       mRgba.release();
   if (mGray != null)
       mGray.release();
   if (mRef != null)
       mRef.release();
   if (mBuff != null)
       mBuff.release();       
   mRgba = null;
   mGray = null;
   mRef = null;
   mBuff = null;
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();  

    MatOfKeyPoint keypointsRef = new MatOfKeyPoint();
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat extractRef = new Mat();
    Mat extract     = new Mat();
    MatOfDMatch matchs = new MatOfDMatch();

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    MatOfDMatch gm = new MatOfDMatch(); 

    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }

    gm.fromList(good_matches); 

    Features2d.drawMatches(mRef, keypointsRef, mGray,
    keypoints, gm, mRgba, new Scalar(255, 0, 0),
    new Scalar(0, 0, 255), new MatOfByte(), 2);       

    LinkedList<Point> objList = new LinkedList<Point>();
    LinkedList<Point> sceneList = new LinkedList<Point>();

    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);

keypointsRef.release();
keypoints.release();
extractRef.release();
extract.release();
matchs.release();
keypointsRef = null;        
keypoints = null;
extractRef = null;
extract = null;
matchs = null;      

  return mRgba;         
}       
}

Any other documentation or reference will be useful. Thanx in advance.

Android application to recognize known object and draw its border in real time camera frame.

I'm really new to openCV I followed this and I dont know what is wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Core.findHomography(object, Calib3d.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem?problem? What are the best algorithms for detection and matching?

Here is my code

public class MainActivity extends Activity implements CvCameraViewListener2 {

    private static final String  TAG = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;

    private Mat              mRgba;
    private Mat              mRef;
    private Mat              mGray;  
    private Mat              mBuff;


    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){

    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
    };


@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.MainActivity);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}


public void onCameraViewStarted(int width, int height) {
     mRgba = new Mat();
     mRef  = new Mat();
     mGray = new Mat(); 
    mBuff = new Mat();  

    try {
        mBuff = Utils.loadResource(this, R.drawable.dartlogo);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }        

public void onCameraViewStopped(){
   if (mRgba != null)
       mRgba.release();
   if (mGray != null)
       mGray.release();
   if (mRef != null)
       mRef.release();
   if (mBuff != null)
       mBuff.release();       
   mRgba = null;
   mGray = null;
   mRef = null;
   mBuff = null;
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();  

    MatOfKeyPoint keypointsRef = new MatOfKeyPoint();
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat extractRef = new Mat();
    Mat extract     = new Mat();
    MatOfDMatch matchs = new MatOfDMatch();

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    MatOfDMatch gm = new MatOfDMatch(); 

    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }

    gm.fromList(good_matches); 

    Features2d.drawMatches(mRef, keypointsRef, mGray,
    keypoints, gm, mRgba, new Scalar(255, 0, 0),
    new Scalar(0, 0, 255), new MatOfByte(), 2);       

    LinkedList<Point> objList = new LinkedList<Point>();
    LinkedList<Point> sceneList = new LinkedList<Point>();

    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);

keypointsRef.release();
keypoints.release();
extractRef.release();
extract.release();
matchs.release();
keypointsRef = null;        
keypoints = null;
extractRef = null;
extract = null;
matchs = null;      

  return mRgba;         
}       
}

Any other documentation or reference will be useful. Thanx in advance.

Android application to recognize known object and draw track object(draw its border border) in real time camera frame.

I'm really new to openCV I followed this and I dont know what is wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Calib3d.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem? What are the best algorithms for detection and matching?

Here is my code

Read object image on start

public class MainActivity extends Activity implements CvCameraViewListener2 {

    private static final String  TAG = "OCVSample::Activity";

    private CameraBridgeViewBase mOpenCvCameraView;

    private Mat              mRgba;
    private Mat              mRef;
    private Mat              mGray;  
    private Mat              mBuff;


    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){

    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
            } break;
            default:
            {
                super.onManagerConnected(status);
            } break;
        }
    }
    };


@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.MainActivity);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

@Override
public void onPause()
{
    super.onPause();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}

public void onDestroy() {
    super.onDestroy();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}


public void onCameraViewStarted(int width, int height) {
     mRgba = new Mat();
     mRef  = new Mat();
     mGray = new Mat(); 
    mBuff = new Mat();  

    try {
        mBuff = Utils.loadResource(this, R.drawable.dartlogo);
R.drawable.test_img);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }        

public void onCameraViewStopped(){
   if (mRgba != null)
       mRgba.release();
   if (mGray != null)
       mGray.release();
   if (mRef != null)
       mRef.release();
   if (mBuff != null)
       mBuff.release();       
   mRgba = null;
   mGray = null;
   mRef = null;
   mBuff = null;
}
 

Processing on live camera input frame

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();  

    MatOfKeyPoint keypointsRef = new MatOfKeyPoint();
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat extractRef = new Mat();
    Mat extract     = new Mat();
    MatOfDMatch matchs = new MatOfDMatch();
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);   

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

//

// Shows error in this line

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    MatOfDMatch gm = new MatOfDMatch(); 

    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }

    gm.fromList(good_matches); 

    Features2d.drawMatches(mRef, keypointsRef, mGray,
    keypoints, gm, mRgba, new Scalar(255, 0, 0),
    new Scalar(0, 0, 255), new MatOfByte(), 2);       

    LinkedList<Point> objList = new LinkedList<Point>();
    LinkedList<Point> sceneList = new LinkedList<Point>();

    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);
 

//

// Shows error in this line
    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);

keypointsRef.release();
keypoints.release();
extractRef.release();
extract.release();
matchs.release();
keypointsRef = null;        
keypoints = null;
extractRef = null;
extract = null;
matchs = null;      

  return mRgba;         
}       
}
mRgba;

Any other documentation or reference will be useful. Thanx in advance.

Android application to recognize known object and track object(draw its border) in real time camera frame.

I'm really new to openCV I followed this and I dont know what is wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Calib3d.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem? What are the best algorithms for detection and matching?

Here is my code

Read object image on start

public void onCameraViewStarted(int width, int height) {
    try {
        mBuff = Utils.loadResource(this, R.drawable.test_img);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }

Processing on live camera input frame

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);   

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

//

// Shows error in this line

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }


    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }


    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

//

// Shows error in this line
    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);


  return mRgba;

Log cat output for size() of each variable

08-06 00:32:28.949: I/OCVSample::Activity(14129): FINAL mRgba Size: 320x240

: mStretch value: 1.0625

: Preview Frame received. Frame size: 115200

: P-. Frame size: 115200

: P-. F-: 115200

: P-. F- 115200

: mGray : 320x240

: mRef : 153x94

: keypoints : 1x442

: keypointsRef : 1x33

: descriptor : 32x442

: descriptorRef : 32x33

: Matches : 1x33

: matchesList : 33

: Max dist : 94.0 -- Min dist : 60.0

: good_matches : 33

: gm : 1x33

: obj Size: 1x33

: scene Size: 1x33

: P-. Frame size: 115200

: P-. F-: 115200

: P-. F-: 115200

: hg Size: 3x3

: perspectiveTransform hg Size: 3x3

: FINAL mRgba Size: 320x240

: mStretch value: 1.0625

All I get as output is the four lines at random places without any proper shape.

Any other documentation or reference will be useful. Thanx in advance.

Android application to recognize known object and track object(draw its border) in real time camera frame.

I'm really new to openCV I followed this and I dont know what is wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Calib3d.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem? What are the best algorithms for detection and matching?

Here is my code

Read object image on start

public void onCameraViewStarted(int width, int height) {
    try {
        mBuff = Utils.loadResource(this, R.drawable.test_img);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }

Processing on live camera input frame

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);   

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

//

// Shows error in this line

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }


    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }


    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

//

// Shows error in this line
    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);


  return mRgba;

Log cat output for size() of each variable

08-06 00:32:28.949: I/OCVSample::Activity(14129): FINAL mRgba Size: 320x240

: mStretch value: 1.0625

: Preview Frame received. Frame size: 115200

: P-. Frame size: 115200

: P-. F-: 115200

: P-. F- 115200

: mGray : 320x240

: mRef : 153x94

: keypoints : 1x442

: keypointsRef : 1x33

: descriptor : 32x442

: descriptorRef : 32x33

: Matches : 1x33

: matchesList : 33

: Max dist : 94.0 -- Min dist : 60.0

: good_matches : 33

: gm : 1x33

: obj Size: 1x33

: scene Size: 1x33

: P-. Frame size: 115200

: P-. F-: 115200

: P-. F-: 115200

: hg Size: 3x3

: perspectiveTransform hg Size: 3x3

: FINAL mRgba Size: 320x240

: mStretch value: 1.0625

All I get as output is the four lines at random places without any proper shape.

Any other documentation or reference will be useful. Thanx in advance.

Android application to recognize known object and track object(draw its border) in real time camera frame.

I'm really new to openCV I followed this and I dont know what is wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Calib3d.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem? What are the best algorithms for detection and matching?

Here is my code

Read object image on start

public void onCameraViewStarted(int width, int height) {
    try {
        mBuff = Utils.loadResource(this, R.drawable.test_img);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }

Processing on live camera input frame

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);   

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

//

// Shows error in this line

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }


    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }


    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

//

// Shows error in this line
     Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);


  return mRgba;

Log cat output for size() of each variable

08-06 00:32:28.949: I/OCVSample::Activity(14129): FINAL mRgba Size: 320x240

320x240 : mStretch value: 1.0625

1.0625 : mGray : 320x240

320x240 : mRef : 153x94

153x94 : keypoints : 1x442

1x442 : keypointsRef : 1x33

1x33 : descriptor : 32x442

32x442 : descriptorRef : 32x33

32x33 : Matches : 1x33

1x33 : matchesList : 33

33 : Max dist : 94.0 -- Min dist : 60.0

60.0 : good_matches : 33

33 : gm : 1x33

1x33 : obj Size: 1x33

1x33 : scene Size: 1x33

1x33 : hg Size: 3x3

: perspectiveTransform hg Size: 3x3

3x3 : FINAL mRgba Size: 320x240

320x240 : mStretch value: 1.0625

1.0625

All I get as output is the four lines at random places without any proper shape.

Any other documentation or reference will be useful. Thanx I get these two error and this when i use back button to exit

08-06 00:32:41.929: E/SurfaceTexture(14129): [unnamed-14129-0] cancelBuffer: slot 5 is not owned by the client (state=0)
08-06 00:32:41.929: E/SurfaceTexture(14129): [unnamed-14129-0] cancelBuffer: slot 6 is not owned by the client (state=2)
08-06 00:32:41.929: W/SurfaceTexture(14129): freeAllBuffersExceptCurrentLocked called but mQueue is not empty
08-06 00:32:42.219: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 00:32:42.259: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x52e40000 size:3350528 offset:3104768
08-06 00:32:42.259: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x53172000 size:3596288 offset:3350528
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x5199d000 size:2457600 offset:1843200
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x51d65000 size:3104768 offset:2490368
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x52c21000 size:1843200 offset:1228800
08-06 00:32:42.309: W/IInputConnectionWrapper(14129): showStatusIcon on inactive InputConnection

The next time I try to launch it via device (or) eclipse I get the following

08-06 01:13:01.239: D/OpenCVManager/Helper(14129): Init finished with status 0
08-06 01:13:01.239: D/OpenCVManager/Helper(14129): Unbind from service
08-06 01:13:01.259: D/OpenCVManager/Helper(14129): Calling using callback
08-06 01:13:01.259: I/OCVSample::Activity(14129): OpenCV loaded successfully
08-06 01:13:01.259: D/JavaCameraView(14129): Connecting to camera
08-06 01:13:01.259: D/JavaCameraView(14129): Initialize java camera
08-06 01:13:01.259: D/JavaCameraView(14129): Trying to open camera with old open()
08-06 01:13:01.689: D/JavaCameraView(14129): getSupportedPreviewSizes()
08-06 01:13:01.699: D/JavaCameraView(14129): Set preview size to 320x240
08-06 01:13:01.769: D/-heap(14129): GC_FOR_ALLOC freed 1136K, 29% free 6737K/9443K, paused 21ms
08-06 01:13:01.769: D/JavaCameraView(14129): startPreview
08-06 01:13:01.779: D/memalloc(14129): /dev/pmem_adsp: Mapped buffer base:0x51b5f000 size:118784, fd:70
''

''
08-06 01:13:01.799: D/memalloc(14129): /dev/pmem_adsp: Mapped buffer base:0x51c47000 size:118784, fd:97
08-06 01:13:01.899: D/JavaCameraView(14129): Starting processing thread
08-06 01:13:01.919: D/memalloc(14129): /dev/pmem: Mapped buffer base:0x52e40000 size:4300800 offset:3686400 fd:100
08-06 01:13:02.039: D/memalloc(14129): /dev/pmem: Mapped buffer base:0x5325a000 size:3657728 offset:3043328 fd:103
08-06 01:13:02.039: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.079: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.109: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.149: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.169: E/cv::error()(14129): OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in advance.void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/stat.cpp, line 1797
08-06 01:13:02.179: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.199: W/dalvikvm(14129): threadid=12: thread exiting with uncaught exception (group=0x40ad59f0)
08-06 01:13:02.199: E/AndroidRuntime(14129): FATAL EXCEPTION: Thread-5390
08-06 01:13:02.199: E/AndroidRuntime(14129): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/stat.cpp:1797: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool)
08-06 01:13:02.199: E/AndroidRuntime(14129): ]
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.features2d.DescriptorMatcher.match_1(Native Method)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.features2d.DescriptorMatcher.match(DescriptorMatcher.java:437)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at com.example.objectrecognition.MainActivity.onCameraFrame(MainActivity.java:197)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:376)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:294)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at java.lang.Thread.run(Thread.java:856)
08-06 01:13:02.219: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.239: D/JavaCameraView(14129): Disconnecting from camera
08-06 01:13:02.239: D/JavaCameraView(14129): Notify thread
08-06 01:13:02.239: D/JavaCameraView(14129): Wating for thread

//////////////////////////////////////////////////////////////////////////////////////////////////// Now the

MainActivity.java:197

is

matcher.match(extractRef, extract, matchs);

all I could understand is that there is some matrix type mismatch error which was not there the first time I run it, I dunno where is the problem.

If i uninstall and try again i get the same problem and sometimes the error points to

Calib3d.findhomography(obj, scene, 8, 2, new Mat());
click to hide/show revision 10
there is always data usage warning for the application in my sony ST23i miro

Android application to recognize known object and track object(draw its border) in real time camera frame.

I'm really new to openCV I followed this and I dont know what is wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Calib3d.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem? What are the best algorithms for detection and matching?

Here is my code

Read object image on start

public void onCameraViewStarted(int width, int height) {
    try {
        mBuff = Utils.loadResource(this, R.drawable.test_img);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }

Processing on live camera input frame

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);   

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

//

// Shows error in this line

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }


    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }


    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);


  return mRgba;

Log cat output for size() of each variable

08-06 00:32:28.949: I/OCVSample::Activity(14129): FINAL mRgba Size: 320x240

: mStretch value: 1.0625

: mGray  : 320x240

: mRef   : 153x94

: keypoints   : 1x442

: keypointsRef   : 1x33

: descriptor : 32x442

: descriptorRef  : 32x33

: Matches : 1x33

: matchesList : 33

: Max dist : 94.0 -- Min dist : 60.0

: good_matches : 33

: gm : 1x33

: obj Size: 1x33

: scene Size: 1x33

: hg Size: 3x3

: FINAL mRgba Size: 320x240

: mStretch value: 1.0625

All I get as output is the four lines at random places without any proper shape.

I get these two error and this when i use back button to exit

08-06 00:32:41.929: E/SurfaceTexture(14129): [unnamed-14129-0] cancelBuffer: slot 5 is not owned by the client (state=0)
08-06 00:32:41.929: E/SurfaceTexture(14129): [unnamed-14129-0] cancelBuffer: slot 6 is not owned by the client (state=2)
08-06 00:32:41.929: W/SurfaceTexture(14129): freeAllBuffersExceptCurrentLocked called but mQueue is not empty
08-06 00:32:42.219: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 00:32:42.259: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x52e40000 size:3350528 offset:3104768
08-06 00:32:42.259: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x53172000 size:3596288 offset:3350528
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x5199d000 size:2457600 offset:1843200
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x51d65000 size:3104768 offset:2490368
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x52c21000 size:1843200 offset:1228800
08-06 00:32:42.309: W/IInputConnectionWrapper(14129): showStatusIcon on inactive InputConnection

The next time I try to launch it via device (or) eclipse I get the following

08-06 01:13:01.239: D/OpenCVManager/Helper(14129): Init finished with status 0
08-06 01:13:01.239: D/OpenCVManager/Helper(14129): Unbind from service
08-06 01:13:01.259: D/OpenCVManager/Helper(14129): Calling using callback
08-06 01:13:01.259: I/OCVSample::Activity(14129): OpenCV loaded successfully
08-06 01:13:01.259: D/JavaCameraView(14129): Connecting to camera
08-06 01:13:01.259: D/JavaCameraView(14129): Initialize java camera
08-06 01:13:01.259: D/JavaCameraView(14129): Trying to open camera with old open()
08-06 01:13:01.689: D/JavaCameraView(14129): getSupportedPreviewSizes()
08-06 01:13:01.699: D/JavaCameraView(14129): Set preview size to 320x240
08-06 01:13:01.769: D/-heap(14129): GC_FOR_ALLOC freed 1136K, 29% free 6737K/9443K, paused 21ms
08-06 01:13:01.769: D/JavaCameraView(14129): startPreview
08-06 01:13:01.779: D/memalloc(14129): /dev/pmem_adsp: Mapped buffer base:0x51b5f000 size:118784, fd:70
''

''
08-06 01:13:01.799: D/memalloc(14129): /dev/pmem_adsp: Mapped buffer base:0x51c47000 size:118784, fd:97
08-06 01:13:01.899: D/JavaCameraView(14129): Starting processing thread
08-06 01:13:01.919: D/memalloc(14129): /dev/pmem: Mapped buffer base:0x52e40000 size:4300800 offset:3686400 fd:100
08-06 01:13:02.039: D/memalloc(14129): /dev/pmem: Mapped buffer base:0x5325a000 size:3657728 offset:3043328 fd:103
08-06 01:13:02.039: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.079: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.109: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.149: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.169: E/cv::error()(14129): OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/stat.cpp, line 1797
08-06 01:13:02.179: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.199: W/dalvikvm(14129): threadid=12: thread exiting with uncaught exception (group=0x40ad59f0)
08-06 01:13:02.199: E/AndroidRuntime(14129): FATAL EXCEPTION: Thread-5390
08-06 01:13:02.199: E/AndroidRuntime(14129): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/stat.cpp:1797: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool)
08-06 01:13:02.199: E/AndroidRuntime(14129): ]
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.features2d.DescriptorMatcher.match_1(Native Method)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.features2d.DescriptorMatcher.match(DescriptorMatcher.java:437)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at com.example.objectrecognition.MainActivity.onCameraFrame(MainActivity.java:197)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:376)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:294)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at java.lang.Thread.run(Thread.java:856)
08-06 01:13:02.219: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.239: D/JavaCameraView(14129): Disconnecting from camera
08-06 01:13:02.239: D/JavaCameraView(14129): Notify thread
08-06 01:13:02.239: D/JavaCameraView(14129): Wating for thread

//////////////////////////////////////////////////////////////////////////////////////////////////// Now the

MainActivity.java:197

is

matcher.match(extractRef, extract, matchs);

all I could understand is that there is some matrix type mismatch error which was not there the first time I run it, I dunno where is the problem.

If i uninstall and try again i get the same problem and sometimes the error points to

Calib3d.findhomography(obj, scene, 8, 2, new Mat());

Android application to recognize known object and track object(draw its border) in real time camera frame.

I'm really new to openCV I followed this and I dont know what is wrong but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Calib3d.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem? What are the best algorithms for detection and matching?

Here is my code

Read object image on start

public void onCameraViewStarted(int width, int height) {
    try {
        mBuff = Utils.loadResource(this, R.drawable.test_img);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }

Processing on live camera input frame

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);   

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    detector.detect(mGray, keypoints);
        detector.detect(mRef, keypointsRef);

    extractor.compute(mGray, keypoints, extract);
        extractor.compute(mRef, keypointsRef, extractRef);

//

// Shows error in this line

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }


    for (int i = 0; i < matchesList.size(); i++) {
        if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            }
    }


    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
        objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
    }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
    obj.fromList(objList);
    scene.fromList(sceneList);

    Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());

    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0, 0});
    obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
    obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
    obj_corners.put(3, 0, new double[] {0, mRef.rows()});

    Core.perspectiveTransform(obj_corners, scene_corners, hg);

    Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
    Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);


  return mRgba;

Log cat output for size() of each variable

08-06 00:32:28.949: I/OCVSample::Activity(14129): FINAL mRgba Size: 320x240

: mStretch value: 1.0625

: mGray  : 320x240

: mRef   : 153x94

: keypoints   : 1x442

: keypointsRef   : 1x33

: descriptor : 32x442

: descriptorRef  : 32x33

: Matches : 1x33

: matchesList : 33

: Max dist : 94.0 -- Min dist : 60.0

: good_matches : 33

: gm : 1x33

: obj Size: 1x33

: scene Size: 1x33

: hg Size: 3x3

: FINAL mRgba Size: 320x240

: mStretch value: 1.0625

All I get as output is the four lines at random places without any proper shape.

I get these two error and this when i use back button to exit

08-06 00:32:41.929: E/SurfaceTexture(14129): [unnamed-14129-0] cancelBuffer: slot 5 is not owned by the client (state=0)
08-06 00:32:41.929: E/SurfaceTexture(14129): [unnamed-14129-0] cancelBuffer: slot 6 is not owned by the client (state=2)
08-06 00:32:41.929: W/SurfaceTexture(14129): freeAllBuffersExceptCurrentLocked called but mQueue is not empty
08-06 00:32:42.219: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 00:32:42.259: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x52e40000 size:3350528 offset:3104768
08-06 00:32:42.259: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x53172000 size:3596288 offset:3350528
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x5199d000 size:2457600 offset:1843200
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x51d65000 size:3104768 offset:2490368
08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x52c21000 size:1843200 offset:1228800
08-06 00:32:42.309: W/IInputConnectionWrapper(14129): showStatusIcon on inactive InputConnection

The next time I try to launch it via device (or) eclipse I get the following

08-06 01:13:01.239: D/OpenCVManager/Helper(14129): Init finished with status 0
08-06 01:13:01.239: D/OpenCVManager/Helper(14129): Unbind from service
08-06 01:13:01.259: D/OpenCVManager/Helper(14129): Calling using callback
08-06 01:13:01.259: I/OCVSample::Activity(14129): OpenCV loaded successfully
08-06 01:13:01.259: D/JavaCameraView(14129): Connecting to camera
08-06 01:13:01.259: D/JavaCameraView(14129): Initialize java camera
08-06 01:13:01.259: D/JavaCameraView(14129): Trying to open camera with old open()
08-06 01:13:01.689: D/JavaCameraView(14129): getSupportedPreviewSizes()
08-06 01:13:01.699: D/JavaCameraView(14129): Set preview size to 320x240
08-06 01:13:01.769: D/-heap(14129): GC_FOR_ALLOC freed 1136K, 29% free 6737K/9443K, paused 21ms
08-06 01:13:01.769: D/JavaCameraView(14129): startPreview
08-06 01:13:01.779: D/memalloc(14129): /dev/pmem_adsp: Mapped buffer base:0x51b5f000 size:118784, fd:70
''

''
08-06 01:13:01.799: D/memalloc(14129): /dev/pmem_adsp: Mapped buffer base:0x51c47000 size:118784, fd:97
08-06 01:13:01.899: D/JavaCameraView(14129): Starting processing thread
08-06 01:13:01.919: D/memalloc(14129): /dev/pmem: Mapped buffer base:0x52e40000 size:4300800 offset:3686400 fd:100
08-06 01:13:02.039: D/memalloc(14129): /dev/pmem: Mapped buffer base:0x5325a000 size:3657728 offset:3043328 fd:103
08-06 01:13:02.039: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.079: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.109: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.149: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.169: E/cv::error()(14129): OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/stat.cpp, line 1797
08-06 01:13:02.179: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.199: W/dalvikvm(14129): threadid=12: thread exiting with uncaught exception (group=0x40ad59f0)
08-06 01:13:02.199: E/AndroidRuntime(14129): FATAL EXCEPTION: Thread-5390
08-06 01:13:02.199: E/AndroidRuntime(14129): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/stat.cpp:1797: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool)
08-06 01:13:02.199: E/AndroidRuntime(14129): ]
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.features2d.DescriptorMatcher.match_1(Native Method)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.features2d.DescriptorMatcher.match(DescriptorMatcher.java:437)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at com.example.objectrecognition.MainActivity.onCameraFrame(MainActivity.java:197)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:376)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:294)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at java.lang.Thread.run(Thread.java:856)
08-06 01:13:02.219: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.239: D/JavaCameraView(14129): Disconnecting from camera
08-06 01:13:02.239: D/JavaCameraView(14129): Notify thread
08-06 01:13:02.239: D/JavaCameraView(14129): Wating for thread

//////////////////////////////////////////////////////////////////////////////////////////////////// Now the

MainActivity.java:197

is

matcher.match(extractRef, extract, matchs);

all I could understand is that there is some matrix type mismatch error which was not there the first time I run it, I dunno where is the problem.

If i uninstall and try again i get the same problem and sometimes the error points to

Calib3d.findhomography(obj, scene, 8, 2, new Mat());

Android application to recognize known object and track object(draw its border) in real time object from camera frame.has more false positives?

I'm really new to openCV I followed this and I dont know what is wrong this but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Calib3d.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem? What are the best algorithms for detection and matching?

Here is my code

Read object image on startits too slow now

public void onCameraViewStarted(int width, int height) {
    try {
        mBuff = Utils.loadResource(this, R.drawable.test_img);
    } catch (IOException e) {
        e.printStackTrace();
    }
    Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
   }

Processing on live mRgba matrix from camera input frame

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {    
    mRgba = inputFrame.rgba();
    Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGBA2GRAY);   

    input
mRef matrix from object image

FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
    DescriptorExtractor.create(DescriptorExtractor.ORB);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
detector.detect(mGray, keypoints);
 detector.detect(mRef, keypointsRef);

    keypointsRef);     
extractor.compute(mGray, keypoints, extract);
 extractor.compute(mRef, keypointsRef, extractRef);

//

// Shows error in this line

    matcher.match(extractRef, extract, matchs);

    List<DMatch> matchesList = matchs.toList();

    Double max_dist = 0.0;
        Double min_dist = 100.0;

    for (int i = 0; i < matchesList.size(); i++) {
        Double dist = (double) matchesList.get(i).distance;
        if (dist < min_dist)
            min_dist = dist;
        if (dist > max_dist)
            max_dist = dist;
    }


    for (int i = 0; i < matchesList.size(); i++) {
        List<KeyPoint> keypoints_RefList = keypointsRef.toList();
List<KeyPoint> keypoints_List = keypoints.toList();
if (matchesList.get(i).distance <= (3 * min_dist)) {
                good_matches.addLast(matchesList.get(i));
            good_matches.addLast(matchesList.get(i));  
}
    }


    List<KeyPoint> keypoints_RefList = keypointsRef.toList();
    List<KeyPoint> keypoints_List = keypoints.toList();

    for (int i = 0; i < good_matches.size(); i++) {
   objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
   sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
 }                 

    MatOfPoint2f obj = new MatOfPoint2f();
    MatOfPoint2f scene = new MatOfPoint2f();
     obj.fromList(objList);
 scene.fromList(sceneList);
 Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());
 Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
 Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);
 obj_corners.put(0, 0, new double[] {0, 0});
 obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
 obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
 obj_corners.put(3, 0, new double[] {0, mRef.rows()});
 Core.perspectiveTransform(obj_corners, scene_corners, hg);
 Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
 Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
 Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
 Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);


  return mRgba;

Log cat output for size() of each variable

08-06 00:32:28.949: I/OCVSample::Activity(14129): FINAL mRgba Size: 320x240

: mStretch value: 1.0625

: mGray  : 320x240

: mRef   : 153x94

: keypoints   : 1x442

: keypointsRef   : 1x33

: descriptor : 32x442

: descriptorRef  : 32x33

: Matches : 1x33

: matchesList : 33

: Max dist : 94.0 -- Min dist : 60.0

: good_matches : 33

: gm : 1x33

: obj Size: 1x33

: scene Size: 1x33

: hg Size: 3x3

: FINAL mRgba Size: 320x240

: mStretch value: 1.0625

All I get This is way too wrong it detects almost anything as output is and draws random points as output.

Is there a way to refine the four lines at random places without any proper shape.

I get these two error and this when i use back button to exit

08-06 00:32:41.929: E/SurfaceTexture(14129): [unnamed-14129-0] cancelBuffer: slot 5 is not owned by detection? 

Can histogram or dialate options be applied to the client (state=0) 08-06 00:32:41.929: E/SurfaceTexture(14129): [unnamed-14129-0] cancelBuffer: slot 6 is not owned by the client (state=2) 08-06 00:32:41.929: W/SurfaceTexture(14129): freeAllBuffersExceptCurrentLocked called but mQueue is not empty 08-06 00:32:42.219: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200 08-06 00:32:42.259: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x52e40000 size:3350528 offset:3104768 08-06 00:32:42.259: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x53172000 size:3596288 offset:3350528 08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x5199d000 size:2457600 offset:1843200 08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x51d65000 size:3104768 offset:2490368 08-06 00:32:42.269: D/memalloc(14129): /dev/pmem: Unmapping buffer base:0x52c21000 size:1843200 offset:1228800 08-06 00:32:42.309: W/IInputConnectionWrapper(14129): showStatusIcon on inactive InputConnection

The next time I try to launch it via device (or) eclipse I get the following

08-06 01:13:01.239: D/OpenCVManager/Helper(14129): Init finished with status 0
08-06 01:13:01.239: D/OpenCVManager/Helper(14129): Unbind from service
08-06 01:13:01.259: D/OpenCVManager/Helper(14129): Calling using callback
08-06 01:13:01.259: I/OCVSample::Activity(14129): OpenCV loaded successfully
08-06 01:13:01.259: D/JavaCameraView(14129): Connecting to camera
08-06 01:13:01.259: D/JavaCameraView(14129): Initialize java camera
08-06 01:13:01.259: D/JavaCameraView(14129): Trying to open camera with old open()
08-06 01:13:01.689: D/JavaCameraView(14129): getSupportedPreviewSizes()
08-06 01:13:01.699: D/JavaCameraView(14129): Set preview size to 320x240
08-06 01:13:01.769: D/-heap(14129): GC_FOR_ALLOC freed 1136K, 29% free 6737K/9443K, paused 21ms
08-06 01:13:01.769: D/JavaCameraView(14129): startPreview
08-06 01:13:01.779: D/memalloc(14129): /dev/pmem_adsp: Mapped buffer base:0x51b5f000 size:118784, fd:70
''

''
08-06 01:13:01.799: D/memalloc(14129): /dev/pmem_adsp: Mapped buffer base:0x51c47000 size:118784, fd:97
08-06 01:13:01.899: D/JavaCameraView(14129): Starting processing thread
08-06 01:13:01.919: D/memalloc(14129): /dev/pmem: Mapped buffer base:0x52e40000 size:4300800 offset:3686400 fd:100
08-06 01:13:02.039: D/memalloc(14129): /dev/pmem: Mapped buffer base:0x5325a000 size:3657728 offset:3043328 fd:103
08-06 01:13:02.039: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.079: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.109: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.149: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.169: E/cv::error()(14129): OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/stat.cpp, line 1797
08-06 01:13:02.179: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.199: W/dalvikvm(14129): threadid=12: thread exiting with uncaught exception (group=0x40ad59f0)
08-06 01:13:02.199: E/AndroidRuntime(14129): FATAL EXCEPTION: Thread-5390
08-06 01:13:02.199: E/AndroidRuntime(14129): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/stat.cpp:1797: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function void cv::batchDistance(cv::InputArray, cv::InputArray, cv::OutputArray, int, cv::OutputArray, int, int, cv::InputArray, int, bool)
08-06 01:13:02.199: E/AndroidRuntime(14129): ]
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.features2d.DescriptorMatcher.match_1(Native Method)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.features2d.DescriptorMatcher.match(DescriptorMatcher.java:437)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at com.example.objectrecognition.MainActivity.onCameraFrame(MainActivity.java:197)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:376)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:294)
08-06 01:13:02.199: E/AndroidRuntime(14129):    at java.lang.Thread.run(Thread.java:856)
08-06 01:13:02.219: D/JavaCameraView(14129): Preview Frame received. Frame size: 115200
08-06 01:13:02.239: D/JavaCameraView(14129): Disconnecting from camera
08-06 01:13:02.239: D/JavaCameraView(14129): Notify thread
08-06 01:13:02.239: D/JavaCameraView(14129): Wating for thread

//////////////////////////////////////////////////////////////////////////////////////////////////// Now the

MainActivity.java:197

is

matcher.match(extractRef, extract, matchs);

all I could understand is that there is some matrix type mismatch error which was not there the first time I run it, I dunno where is the problem.

If i uninstall and try again i get the same problem and sometimes the error points to

Calib3d.findhomography(obj, scene, 8, 2, new Mat());
images?

Android application to recognize and track object from camera has more false positives?

I'm really new to openCV I followed this this but its detection too slow nowslow

mRgba matrix from camera input
mRef matrix from object image

FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
detector.detect(mGray, keypoints);
detector.detect(mRef, keypointsRef);     
extractor.compute(mGray, keypoints, extract);
extractor.compute(mRef, keypointsRef, extractRef);
matcher.match(extractRef, extract, matchs);
List<KeyPoint> keypoints_RefList = keypointsRef.toList();
List<KeyPoint> keypoints_List = keypoints.toList();
if (matchesList.get(i).distance <= (3 * min_dist)) {
 good_matches.addLast(matchesList.get(i));  
}
for (int i = 0; i < good_matches.size(); i++) {
  objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
  sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
}  
obj.fromList(objList);
scene.fromList(sceneList);
Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());
Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);
obj_corners.put(0, 0, new double[] {0, 0});
obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
obj_corners.put(3, 0, new double[] {0, mRef.rows()});
Core.perspectiveTransform(obj_corners, scene_corners, hg);
Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);

This is way too wrong it detects almost anything as output and draws random points as output.

Is there a way to refine the detection?

Can histogram histogram, dialate or dialate smooth options be applied to the images?

Android application to recognize and track object from camera has more false positives?

I'm really new to openCV I followed this but detection too slow

mRgba matrix from camera input
mRef matrix from object image

FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
detector.detect(mGray, keypoints);
detector.detect(mRef, keypointsRef);     
extractor.compute(mGray, keypoints, extract);
extractor.compute(mRef, keypointsRef, extractRef);
matcher.match(extractRef, extract, matchs);
List<KeyPoint> keypoints_RefList = keypointsRef.toList();
List<KeyPoint> keypoints_List = keypoints.toList();
if (matchesList.get(i).distance <= (3 * min_dist)) {
 good_matches.addLast(matchesList.get(i));  
}
for (int i = 0; i < good_matches.size(); i++) {
  objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
  sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
}  
obj.fromList(objList);
scene.fromList(sceneList);
Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());
Mat obj_corners = new Mat(4,1,CvType.CV_32FC2); 
Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);
obj_corners.put(0, 0, new double[] {0, 0});
obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
obj_corners.put(3, 0, new double[] {0, mRef.rows()});
Core.perspectiveTransform(obj_corners, scene_corners, hg);
Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);

This is way too wrong it detects almost anything as output and draws random points as output.

Is there a way to refine the detection?

Can histogram, dialate or smooth options be applied to the images?