2012-03-23 1 views
2

SIFT, SURF, BRIEF 및 ORB의 기능을 보여주는 간단한 애플리케이션 (sample2 수정)을 구현하고 싶습니다. 사용자는 단순히 회전 또는 스케일 불변성 또는 속도를 비교할 수 있습니다. 그러나 나는 당신을 도울 수 있도록 내가 할 수없는 실패를 발견했습니다. 내가 SIFT 또는 SURF를 사용하려고하면 I가 일치 할 때 난 항상 라인에 예외를 얻을 :패턴 인식 알고리즘으로 예외 얻기 ANDROID에 대한 OpenCV에서 SIUR, SURF, SIFT

matcherBruteForce.match(descriptorFrame, matches); 내가 비슷한 AR 응용 프로그램이 이러한 설정이 작동, 그래서 내가 실수를 해요 어디 알아낼 수 없습니다 . 변수 "matcherBruteForce"를 BRUTEFORCE, BRUTEFORCE_L1, BRUTEFORCE_SL2 이벤트를 BRUTEFORCE_HAMMING으로 설정하려고했습니다. 하지만 난 항상 같은 예외를 가지고 :

SIFT :

CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/features2d/include/opencv2/features2d/features2d.hpp:2455: error: (-215) DataType<ValueType>::type == matcher.trainDescCollection[iIdx].type() || matcher.trainDescCollection[iIdx].empty() in function static void cv::BruteForceMatcher<Distance>::commonKnnMatchImpl(cv::BruteForceMatcher<Distance>&, const cv::Mat&, std::vector<std::vector<cv::DMatch> >&, int, const std::vector<cv::Mat>&, bool) [with Distance = cv::SL2<float>] 
] 

SURF :

어떤 도움이

감사
CvException [org.opencv.core.CvException: /home/andreyk/OpenCV2/trunk/opencv_2.3.1.b2/modules/features2d/include/opencv2/features2d/features2d.hpp:2455: error: (-215) DataType<ValueType>::type == matcher.trainDescCollection[iIdx].type() || matcher.trainDescCollection[iIdx].empty() in function static void cv::BruteForceMatcher<Distance>::commonKnnMatchImpl(cv::BruteForceMatcher<Distance>&, const cv::Mat&, std::vector<std::vector<cv::DMatch> >&, int, const std::vector<cv::Mat>&, bool) [with Distance = cv::SL2<float>] 
] 

전체 클래스 : 당신이

package sk.bolyos.opencv; 

import java.util.Vector; 

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.highgui.VideoCapture; 
import org.opencv.android.Utils; 
import org.opencv.core.Mat; 
import org.opencv.core.Size; 
import org.opencv.imgproc.Imgproc; 
import org.opencv.highgui.Highgui; 

import sk.bolyos.svk.*; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.util.Log; 
import android.view.SurfaceHolder; 




public class MyView extends CvViewBase { 

    private static final int BOUNDARY = 35; 

    private Mat mRgba; 
    private Mat mGray; 
    private Mat mIntermediateMat; 
    private Mat mLogoMilka1,mLogoMilka2,mLogoMilka3,mLogoMilka4; 
    ///////////////////DETECTORS 
    FeatureDetector siftDetector = FeatureDetector.create(FeatureDetector.SIFT); 
    FeatureDetector surfDetector = FeatureDetector.create(FeatureDetector.SURF); 
    FeatureDetector fastDetector = FeatureDetector.create(FeatureDetector.FAST); 
    FeatureDetector orbDetector = FeatureDetector.create(FeatureDetector.ORB); 
    ///////////////////DESCRIPTORS 
    DescriptorExtractor siftDescriptor = DescriptorExtractor.create(DescriptorExtractor.SIFT); 
    DescriptorExtractor surfDescriptor = DescriptorExtractor.create(DescriptorExtractor.SURF); 
    DescriptorExtractor briefDescriptor = DescriptorExtractor.create(DescriptorExtractor.BRIEF); 
    DescriptorExtractor orbDescriptor = DescriptorExtractor.create(DescriptorExtractor.ORB); 
    ///////////////////DATABASE 
    Vector<KeyPoint> vectorMilka1 = new Vector<KeyPoint>(); 
    Vector<KeyPoint> vectorMilka2 = new Vector<KeyPoint>(); 
    Vector<KeyPoint> vectorMilka3 = new Vector<KeyPoint>(); 
    Vector<KeyPoint> vectorMilka4 = new Vector<KeyPoint>(); 
    Mat descriptorMilka1 = new Mat(); 
    Mat descriptorMilka2 = new Mat(); 
    Mat descriptorMilka3 = new Mat(); 
    Mat descriptorMilka4 = new Mat(); 
    ///////////////////VIDEO 
    Vector<KeyPoint> vectorFrame = new Vector<KeyPoint>(); 
    Mat descriptorFrame = new Mat(); 

    DescriptorMatcher matcherHamming = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT); 
    DescriptorMatcher matcherBruteForce = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_SL2); 
    Vector<DMatch> matches = new Vector<DMatch>(); 
    Vector<Mat> siftDescriptors = new Vector<Mat>(); 
    Vector<Mat> surfDescriptors = new Vector<Mat>(); 
    Vector<Mat> briefDescriptors = new Vector<Mat>(); 
    Vector<Mat> orbDescriptors = new Vector<Mat>(); 

    public MyView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     try{ 
      /* 
      if (mLogoMilka1 == null){ 
       mLogoMilka1 = new Mat(); 
       mLogoMilka1 = Utils.loadResource(getContext(), R.drawable.milkalogo); 
       fillDB(mLogoMilka1,vectorMilka1,descriptorMilka1); 
      } 
      if (mLogoMilka2 == null){ 
       mLogoMilka2 = new Mat(); 
       mLogoMilka2 = Utils.loadResource(getContext(), R.drawable.milkalogom); 
       fillDB(mLogoMilka2,vectorMilka2,descriptorMilka2); 
      } 
      if (mLogoMilka3 == null){ 
       mLogoMilka3 = new Mat(); 
       mLogoMilka3 = Utils.loadResource(getContext(), R.drawable.milkalogol); 
       fillDB(mLogoMilka3,vectorMilka3,descriptorMilka3); 
      }*/ 
      if (mLogoMilka4 == null){ 
       mLogoMilka4 = new Mat(); 
       mLogoMilka4 = Utils.loadResource(getContext(), R.drawable.milkalogolc); 
       fillDB(mLogoMilka4,vectorMilka4,descriptorMilka4); 
      } 

     }catch(Exception e){ 
      Log.e("SVK APPLICATION", "in MyView constructor "+e.toString()); 
     } 
    } 

    public void fillDB(Mat mLogo,Vector<KeyPoint> vector,Mat descriptor){ 

     //SIFT 
     siftDetector.detect(mLogo, vector); 
     siftDescriptor.compute(mLogo, vector, descriptor); 
     siftDescriptors.add(descriptor); 
     //SURF 
     surfDetector.detect(mLogo, vector); 
     surfDescriptor.compute(mLogo, vector, descriptor); 
     surfDescriptors.add(descriptor); 
     //FAST+BRIEF 
     fastDetector.detect(mLogo, vector); 
     briefDescriptor.compute(mLogo, vector, descriptor); 
     briefDescriptors.add(descriptor); 
     //ORB 
     orbDetector.detect(mLogo, vector); 
     orbDescriptor.compute(mLogo, vector, descriptor); 
     orbDescriptors.add(descriptor); 

    } 


    @Override 
    public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { 
     super.surfaceChanged(_holder, format, width, height); 

     synchronized (this) { 
      // initialize Mats before usage 
      mGray = new Mat(); 
      mRgba = new Mat(); 
      mIntermediateMat = new Mat(); 
      matches = new Vector<DMatch>(); 
      vectorFrame = new Vector<KeyPoint>(); 
      descriptorFrame = new Mat(); 
     } 
    } 

    @Override 
    protected Bitmap processFrame(VideoCapture capture) { 
     // TODO Auto-generated method stub 
     switch (SVKApplikaciaActivity.viewMode) { 
     case SVKApplikaciaActivity.VIEW_MODE_SIFT: 
      //TODO SIFT 
      try{ 
       //matcherBruteForce = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE); 
       //matcherBruteForce.clear(); 
       matcherBruteForce.add(siftDescriptors); 
       matcherBruteForce.train();// proba 

       capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); 
       Imgproc.resize(mGray, mGray,new Size(480,320)); 
       siftDetector.detect(mGray, vectorFrame); 
       siftDescriptor.compute(mGray, vectorFrame, descriptorFrame); 

       matcherBruteForce.match(descriptorFrame, matches); 
       Vector<DMatch> matchesXXX = new Vector<DMatch>(); 
       for (DMatch t : matches) 
        if(t.distance<BOUNDARY) 
         matchesXXX.add(t); 
       Mat nGray = new Mat(); 
       Mat nLogo = new Mat(); 
       Mat nRgba = new Mat(); 
       Imgproc.cvtColor(mGray, nGray, Imgproc.COLOR_RGBA2RGB, 3); 
       Imgproc.cvtColor(mLogoMilka4, nLogo, Imgproc.COLOR_RGBA2BGR, 3); 
       Features2d.drawMatches(nGray, vectorFrame, nLogo, vectorMilka4, matchesXXX, nRgba); 
       Imgproc.cvtColor(nRgba, mRgba, Imgproc.COLOR_RGB2RGBA, 4); 
      }catch(Exception e){ 
       Log.e("SVK APPLICATION","in SIFT "+ e.toString()); 
      } 
      break; 
     case SVKApplikaciaActivity.VIEW_MODE_SURF: 
      //TODO SURF 
      try{ 
       //matcherBruteForce = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE); 
       //matcherBruteForce.clear(); 
       matcherBruteForce.add(surfDescriptors); 
       matcherBruteForce.train();// proba 

       capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); 
       Imgproc.resize(mGray, mGray,new Size(480,320)); 
       surfDetector.detect(mGray, vectorFrame); 
       surfDescriptor.compute(mGray, vectorFrame, descriptorFrame); 

       matcherBruteForce.match(descriptorFrame, matches); 
       Vector<DMatch> matchesXXX = new Vector<DMatch>(); 
       for (DMatch t : matches) 
        if(t.distance<BOUNDARY) 
         matchesXXX.add(t); 
       Mat nGray = new Mat(); 
       Mat nLogo = new Mat(); 
       Mat nRgba = new Mat(); 
       Imgproc.cvtColor(mGray, nGray, Imgproc.COLOR_RGBA2RGB, 3); 
       Imgproc.cvtColor(mLogoMilka4, nLogo, Imgproc.COLOR_RGBA2BGR, 3); 
       Features2d.drawMatches(nGray, vectorFrame, nLogo, vectorMilka4, matchesXXX, nRgba); 
       Imgproc.cvtColor(nRgba, mRgba, Imgproc.COLOR_RGB2RGBA, 4); 
      }catch(Exception e){ 
       Log.e("SVK APPLICATION","in Surf "+ e.toString()); 
      } 
      break; 
     case SVKApplikaciaActivity.VIEW_MODE_BRIEF: 
      //TODO BRIEF 
      try{ 
       matcherHamming = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT); 
       matcherHamming.add(briefDescriptors); 
       matcherHamming.train();// proba 

       capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); 
       Imgproc.resize(mGray, mGray,new Size(480,320)); 
       fastDetector.detect(mGray, vectorFrame); 
       briefDescriptor.compute(mGray, vectorFrame, descriptorFrame); 

       matcherHamming.match(descriptorFrame, matches); 
       Vector<DMatch> matchesXXX = new Vector<DMatch>(); 
       for (DMatch t : matches) 
        if(t.distance<BOUNDARY) 
         matchesXXX.add(t); 
       Mat nGray = new Mat(); 
       Mat nLogo = new Mat(); 
       Mat nRgba = new Mat(); 
       Imgproc.cvtColor(mGray, nGray, Imgproc.COLOR_RGBA2RGB, 3); 
       Imgproc.cvtColor(mLogoMilka4, nLogo, Imgproc.COLOR_RGBA2BGR, 3); 
       Features2d.drawMatches(nGray, vectorFrame, nLogo, vectorMilka4, matchesXXX, nRgba); 
       Imgproc.cvtColor(nRgba, mRgba, Imgproc.COLOR_RGB2RGBA, 4); 
      }catch(Exception e){ 
       Log.e("SVK APPLICATION","in Brief "+ e.toString()); 
      } 
      break; 
     case SVKApplikaciaActivity.VIEW_MODE_ORB: 
      //TODO ORB 
      try{ 
       matcherHamming = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMINGLUT); 
       matcherHamming.add(orbDescriptors); 
       matcherHamming.train();// proba 

       capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA); 
       Imgproc.resize(mGray, mGray,new Size(480,320)); 
       orbDetector.detect(mGray, vectorFrame); 
       orbDescriptor.compute(mGray, vectorFrame, descriptorFrame); 

       matcherHamming.match(descriptorFrame, matches); 
       Vector<DMatch> matchesXXX = new Vector<DMatch>(); 
       for (DMatch t : matches) 
        if(t.distance<BOUNDARY) 
         matchesXXX.add(t); 
       Mat nGray = new Mat(); 
       Mat nLogo = new Mat(); 
       Mat nRgba = new Mat(); 
       Imgproc.cvtColor(mGray, nGray, Imgproc.COLOR_RGBA2RGB, 3); 
       Imgproc.cvtColor(mLogoMilka4, nLogo, Imgproc.COLOR_RGBA2BGR, 3); 
       Features2d.drawMatches(nGray, vectorFrame, nLogo, vectorMilka4, matchesXXX, nRgba); 
       Imgproc.cvtColor(nRgba, mRgba, Imgproc.COLOR_RGB2RGBA, 4); 
       }catch(Exception e){ 
        Log.e("SVK APPLICATION","in ORB "+ e.toString()); 
       } 
      break; 
     case SVKApplikaciaActivity.VIEW_MODE_AR: 
      //TODO AR 
      break;  

     } 

     Bitmap bmp = Bitmap.createBitmap(mRgba.cols(), mRgba.rows(), Bitmap.Config.ARGB_8888); 

     if (Utils.matToBitmap(mRgba, bmp)) 
      return bmp; 

     bmp.recycle(); 

     return null; 
    } 

    @Override 
    public void run() { 
     super.run(); 

     synchronized (this) { 
      // Explicitly deallocate Mats 
      if (mRgba != null) 
       mRgba.release(); 
      if (mGray != null) 
       mGray.release(); 
      if (mIntermediateMat != null) 
       mIntermediateMat.release(); 

      mRgba = null; 
      mGray = null; 
      mIntermediateMat = null; 
     } 
    } 

} 

답변

3

I 곳 Log.e("SVK APPLICATION","vectorFrame size = "+ vectorFrame.size());을 넣어 잘되지 않을 때, 나는 그것이 제로 vectorFrame을 반환 생각 내가 그 문제를 안다고 생각해. 당신이 사용하고있는 matcher는 SIFT와 SURF 서술자에 적용될 수 없다. 당신은 체로 치거나 서핑으로 DescriptorMatcher를 사용해야하는 경우, 당신은 당신이 그것을 해밍로 설정 DescriptorMatcher을 통과하는 경우는, 오류를 반환합니다, SURF 이후 같은

DescriptorMatcher matcherBruteForce=DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_SL2); 

로 설정하고 FloatBased 독점적 디스크립터 동의 SIFT해야합니다.

코드에 두 개의 DescriptorMatchers가 있으며 하나는 BRUTEFORCE.SL2로 설정되고 다른 하나는 HAMMING으로 설정됩니다. 올바른 것, 즉 BRUTEFORCE.SL2를 SIFT 또는 SURF로 전달하십시오. 여기 http://computer-vision-talks.com/2011/07/comparison-of-the-opencvs-feature-detection-algorithms-ii/

을 그것에 대해 더 읽기가 키포인트의 높은 숫자를 추출하기 때문에 ORB에 비해

그것의, 그러나 가장은 SIFT 또는 SURF위한 플란넬 기반 매처 (matcher)를 사용하고, 플란넬은 키포인트의 대형 세트에 적합 여기에 http://opencv.willowgarage.com/documentation/cpp/flann_fast_approximate_nearest_neighbor_search.html

업데이트 : 일치하는 uchar 설명자에 L2 또는 L1 거리를 사용할 수 있습니다. 당신이 BRUTEFORCE로 설정된 DescriptorMatcher를 넘긴다면 ORB에서도 작동 할 수 있습니다. (결과가 좋지는 않지만)

+0

나는 그 이유를 해결했지만, 그것은 bruteforce matcher가 이미 SL2이기 때문에 당신의 대답이 아닙니다. 그 이유는 서핑 오브 서핑 설명 서를 별도로 설정하지 않았기 때문입니다. 하지만 고마워. – Csabi

+0

그래 .. 나도 그걸 깨달았다. 그래도 별개의 표제 (orb descriptor)를 설정하지 않았다는 것을 무엇을 의미합니까? –

+0

필자는 fillDB 함수를 사용하지 않고 모든 ORB/BRIEF/SURF/SIFT에 별도의 디스크립터를 사용합니다. – Csabi

1

있습니다 너의 크기 vectorFrame이 0이 아닙니다. 내가 같은 문제 .. 문제가 검출 알고리즘으로에있을 것입니다 있다고 생각 이미지의 색상 코드가

그냥