recognizing objects c++ Android

asked 2013-03-22 06:10:32 -0500

Marwen Z gravatar image

updated 2013-03-22 06:26:20 -0500

Hi , i just copied the code Features2D + Homography to find a known object from http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html#feature-homography
on my Android project 2 inupts : a known object(Bitmap from drawable and i convert it to Mat) and an input frame but in Logcat it show me "Preview Frame received. Need to create MAT and deliver it to clients" BLACK FRAME

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/nonfree/nonfree.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include<stdio.h>
#include <android/log.h>

#define LOG_TAG "native"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
using namespace cv;
cv::Mat tmp, nomask, mask;

extern "C" {
JNIEXPORT void JNICALL Java_com_example_features_MainActivity_setup(JNIEnv*, jobject,
                                                                    jint rows, jint cols) {
  tmp.create(rows, cols, CV_8UC1);
  nomask = cv::Mat();
  mask = cv::Mat::zeros(cv::Size(cols, rows), CV_8UC1); // swap rows <-> cols
  cv::Mat roi(mask, cv::Rect(cols/2 - cols/4, rows/2 - rows/4, cols/2, rows/2));
  LOGI("rect: (%d %d)", roi.rows, roi.cols);
  roi = cv::Scalar(255);
}

JNIEXPORT void JNICALL Java_com_example_features_MainActivity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
{

        Mat& img_object  = *(Mat*)addrGray;
        Mat& img_scene = *(Mat*)addrRgba;
        int minHessian = 400;

          OrbFeatureDetector detector( minHessian );

        vector<KeyPoint> keypoints_object, keypoints_scene;

          detector.detect( img_object, keypoints_object );
          detector.detect( img_scene, keypoints_scene );

          //-- Step 2: Calculate descriptors (feature vectors)
          OrbDescriptorExtractor extractor;

          Mat descriptors_object, descriptors_scene;

          extractor.compute( img_object, keypoints_object, descriptors_object );
          extractor.compute( img_scene, keypoints_scene, descriptors_scene );

          //-- Step 3: Matching descriptor vectors using FLANN matcher
          FlannBasedMatcher   matcher;
          vector< DMatch > matches;
       matcher.match( descriptors_object, descriptors_scene, matches );

       //  drawKeypoints(img_scene, keypoints_scene,img_scene);
        double max_dist = 0; double min_dist = 100;

          //-- Quick calculation of max and min distances between keypoints
          for( int i = 0; i < descriptors_object.rows; i++ )
          { double dist = matches[i].distance;
            if( dist < min_dist ) min_dist = dist;
            if( dist > max_dist ) max_dist = dist;
          }



          //-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
          vector< DMatch > good_matches;

          for( int i = 0; i < descriptors_object.rows; i++ )
          { if( matches[i].distance < 3*min_dist )
             { good_matches.push_back( matches[i]); }
          }

          Mat img_matches;
          drawMatches( img_object, keypoints_object, img_scene, keypoints_scene,
                       good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
                       vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

          //-- Localize the object
          vector<Point2f> obj;
          vector<Point2f> scene;

          for( int i = 0; i < good_matches.size(); i++ )
          {
            //-- Get the keypoints from the good matches
            obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
            scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
          }

     //   Mat H = findHomography( obj, scene, CV_RANSAC );

          //-- Get the corners from the image_1 ( the object to be "detected" )
         vector<Point2f> obj_corners(4);
          obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
          obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
          vector<Point2f> scene_corners(4);

     //     perspectiveTransform( obj_corners, scene_corners, H);

          //-- Draw lines between the corners (the mapped object in the scene - image_2 )
          line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
          line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0 ...
(more)
edit retag flag offensive close merge delete

Comments

1

Actually, there isn't a question. Please state the obvious more clearly.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-03-22 07:35:08 -0500 )edit