17

I'm writing code to find similar object from drawable in camerapreview. I am using the newest Opencv 2.4.4.

Below are my functions and an output from logcat. What am I doing wrong that I'm getting such output?

 public void detect_image (Mat mRgba) {

    object_desc = new Mat();
    scene_desc = new Mat();

    object_keys = new MatOfKeyPoint();
    scene_keys = new MatOfKeyPoint();

    matches = new MatOfDMatch();
    good_matches = new MatOfDMatch();

    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.sto);
    Utils.bitmapToMat(image,object);   

    surf = FeatureDetector.create(FeatureDetector.FAST);
    surf.detect( object, object_keys );   
    surf.detect( mRgba, scene_keys);
    surfEX = DescriptorExtractor.create(DescriptorExtractor.BRIEF);
    surfEX.compute(object, object_keys, object_desc);       
    surfEX.compute(mRgba, scene_keys, scene_desc);
    dm = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_SL2);    
    dm.match(object_desc, scene_desc, matches);

    double max_dist = 0;
    double min_dist = 100;

    for( int i = 0; i < object_desc.rows(); i++ )
      { double dist = matches.toArray()[i].distance;
        if( dist < min_dist ) min_dist = dist;
        if( dist > max_dist ) max_dist = dist;
      }

     for( int i = 0; i < object_desc.rows(); i++ )
      {  MatOfDMatch temp = new MatOfDMatch();
         if( matches.toArray()[i].distance < 3*min_dist )
         {   temp.fromArray(matches.toArray()[i]);
             good_matches.push_back(temp); 
             }        
      }

}



public Mat onCameraFrame(CvCameraViewFrame inputFrame) {        
     mRgba = inputFrame.rgba();
             detect_image(mRgba);
     return inputFrame.rgba();
}

Logcat:

03-27 01:55:31.258: E/cv::error()(564): 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/50-SDK/opencv/modules/core/src/stat.cpp,
line 1803
pocmo
  • 660
  • 6
  • 24
Martus0
  • 667
  • 1
  • 10
  • 16
  • Which line exactly is failing on this assertion? Are you certain that `surf.detect()` can accept a Bitmap as input? Shouldn't it be a Mat? – JonasVautherin Mar 27 '13 at 06:42
  • surf.detect() takes Mats and object and mRgba are Mats. The line below causes the problems: dm.match(object_desc, scene_desc, matches); – Martus0 Mar 27 '13 at 10:40
  • 3
    My mistake, I had not seen `Utils.bitmapToMat()`. Did you verify manually that `object_desc.type == scene_desc.type` and that `object_desc.cols == object_scene.cols`? There apparently is some kind of incompatibility between your `object_desc` and `scene_desc`... – JonasVautherin Mar 27 '13 at 12:41
  • Thank you very much in first frame `object_desc.cols() == scene_desc.cols()` was false and then it is true. Simple 'if' has resolved the problem :) – Martus0 Mar 27 '13 at 20:23

2 Answers2

20

Just for the sake of closing this question:

According to your comment, the following line was causing the problem:

dm.match(object_desc, scene_desc, matches);

I advised you to manually verify that:

(object_desc.type == scene_desc.type &&
 object_desc.cols == object_scene.cols)

The problem was eventually that for the first frame, object_desc.cols() != scene_desc.cols(). A simple if was enough to solve the problem.

Sam R.
  • 16,027
  • 12
  • 69
  • 122
JonasVautherin
  • 7,297
  • 6
  • 49
  • 95
  • You sir, are my hero. Took two days to work this out! +1 – Liam George Betsworth Jun 04 '14 at 09:49
  • Open Cv Mat doestn't have type method now would you please update this answer – Crawler Jun 08 '16 at 07:28
  • hey @LiamGeorgeBetsworth.... can you please tell me how did you solve this, because I'm using following approach and error is still there if (thisHisto.type() == toCompareHisto.type() && thisHisto.cols() == toCompareHisto.cols()) { matcher.match(thisHisto, toCompareHisto, matches); } – AwaisMajeed Aug 23 '17 at 07:30
  • @LiamGeorgeBetsworth I'd be really grateful sir....I'm stuck with this for a long time – AwaisMajeed Aug 23 '17 at 07:31
0

(Open Cv Mat doestn't have type method) Try this method..

public  void match(Mat object_desc, Mat scene_desc, MatOfDMatch matches)
{
    if(object_desc.type() == scene_desc.type() &&
            object_desc.cols() == scene_desc.cols()) {            
        match_1(nativeObj, object_desc.nativeObj, scene_desc.nativeObj, matches.nativeObj);
    }

}
Shyam Kumar
  • 909
  • 11
  • 12