错误的匹配与ORB在安卓 [英] Error matching with ORB in Android

查看:209
本文介绍了错误的匹配与ORB在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code的工作很好,但是当它提取关键点,它匹配不佳的两个图像。 在这里,你可以找到我的code,但我不知道该如何绘制 JAVA Android的良好匹配

 描述符=新垫();
        关键点=新MatOfKeyPoint();
        探测器= FeatureDetector.create(FeatureDetector.ORB);
        detector.detect(IMG1,关键点);
        描述= DescriptorExtractor.create(DescriptorExtractor.ORB);
        descriptor.compute(IMG1,关键点,描述符);
        匹配= DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
     ColorDetection.cvt_YUVtoRGBtoHSV(mYuv,mGraySubmat);
          MatOfKeyPoint mKeyPoints =新MatOfKeyPoint();
        MatOfDMatch匹配=新MatOfDMatch();

          detector.detect(mGraySubmat,mKeyPoints);
          descriptor.compute(mGraySubmat,mKeyPoints,mIntermediateMat);

        matcher.match(mIntermediateMat,描述符,匹配);
        mIntermediateMat2.create(resultSize,CvType.CV_8UC1);
        Features2d.drawMatches(IMG1,关键点,mGraySubmat,mKeyPoints,比赛中,
                mIntermediateMat2,绿色,红色,MATCH_MASK,Features2d.NOT_DRAW_SINGLE_POINTS);

          Imgproc.resize(mIntermediateMat2,mIntermediateMat2,mRgba.size());
          Imgproc.cvtColor(mIntermediateMat2,mRgba,Imgproc.COLOR_RGBA2BGRA,4);
     Utils.matToBitmap(mRgba,BMP);

      DMatch DM [] = matches.toArray();
          名单<点> LP1 =新的ArrayList<点>(dm.length);
          名单<点> LP2 =新的ArrayList<点>(dm.length);
          关键点TKP [] = keypoints.toArray();
          关键点QKP [] = mKeyPoints.toArray();
          的for(int i = 0; I< dm.length;我++){
              DMatch DMA = DM [I]
              lp1.add(TKP [dma.trainIdx] .PT);
              lp2.add(QKP [dma.queryIdx] .PT);
          }
          MatOfPoint2f点preV =新MatOfPoint2f(lp1.toArray(新点[0]));
          MatOfPoint2f pointsAct =新MatOfPoint2f(lp2.toArray(新点[0]));
        Log.i(点preV,点prev.size()的toString());
        Log.i(pointsAct,pointsAct.size()的toString());
          fundamental_matrix.create(resultSize,CvType.CV_8UC1);
        fundamental_matrix = Calib3d.findFundamentalMat(
                  pointsAct,点preV,Calib3d.FM_RANSAC,3,0.99);
 

任何建议?

编辑:

我不能转换火柴名单!因为 Feature2d.drawMatches() 需要一个 MatOfDmatch ,而不是名单,其中,Dmatch>

  MatOfDMatch匹配,matches12,matches21;
matcher.match(descriptors1,descriptors2,matches12);
matcher.match(descriptors2,descriptors1,matches21);

迭代matches12
    DMatch向前= matches12 [I]
    DMatch落后= matches21 [forward.trainIdx]
    如果(backward.trainIdx == forward.queryIdx)
 //着增加比赛
Features2d.drawMatches(IMG1,关键点,mGraySubmat,mKeyPoints,火柴,mIntermediateMat2);
 

解决方案

您code应该是这样的:

  FeatureDetector探测器= FeatureDetector.create(FeatureDetector.ORB);
 DescriptorExtractor描述符= DescriptorExtractor.create(DescriptorExtractor.ORB);;
 DescriptorMatcher匹配= DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);

 //第一个图像
 垫IMG1 = Highgui.imread(<此搜索路径>中);
 垫descriptors1 =新垫();
 MatOfKeyPoint keypoints1 =新MatOfKeyPoint();

 detector.detect(IMG1,keypoints1);
 descriptor.compute(IMG1,keypoints1,descriptors1);

 //第二图像
 垫IMG2 = Highgui.imread(< IMAGE2路径>中);
 垫descriptors2 =新垫();
 MatOfKeyPoint keypoints2 =新MatOfKeyPoint();

 detector.detect(IMG2,keypoints2);
 descriptor.compute(IMG2,keypoints2,descriptors2);

 //匹配应该包括2个不同形象的描述
 MatOfDMatch匹配=新MatOfDMatch();
 matcher.match(descriptors1,descriptors2,匹配);
 //功能和连接的颜色
 标红=新的标量(255,0,0);
 标绿色=新的标量(0,255,0);
 //输出图像
 垫outputImg =新垫();
 MatOfByte drawnMatches =新MatOfByte();
 //这将吸引所有的比赛,工作正常
 Features2d.drawMatches(IMG1,keypoints1,IMG2,keypoints2,火柴,
 outputImg,绿色,红色,drawnMatches,Features2d.NOT_DRAW_SINGLE_POINTS);
 

另外,如果你想只显示功能,您可以添加这个code:

 垫featuredImg =新垫();
 标量kpColor =新的标量(255,159,10); //这将是关键点颜色
 // featuredImg将第一图像的输出
 Features2d.drawKeypoints(IMG1,keypoints1,featuredImg,kpColor,0);
 // featuredImg将第一图像的输出
 Features2d.drawKeypoints(IMG1,keypoints1,featuredImg,kpColor,0);
 

然后就可以显示匹配点是这样的:

 位图imageMatched = Bitmap.createBitmap(outputImg.cols(),outputImg.rows(),Bitmap.Config.RGB_565); //需要保存的位图
  Utils.matToBitmap(outputImg,imageMatched);
  ImageView.setImageBitmap(imageMatched);
 

最后,你可以实现良好的匹配。我希望这个线程会有所帮助。

My code is working good but when it extracts keypoints, it matches poorly the two images. Here you can find my code, but I don't know how to draw good matched in JAVA Android

 descriptors = new Mat();
        keypoints = new MatOfKeyPoint();
        detector = FeatureDetector.create(FeatureDetector.ORB);
        detector.detect(img1, keypoints);
        descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
        descriptor.compute(img1, keypoints, descriptors);
        matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
     ColorDetection.cvt_YUVtoRGBtoHSV(mYuv,mGraySubmat);
          MatOfKeyPoint mKeyPoints = new MatOfKeyPoint();
        MatOfDMatch  matches = new MatOfDMatch();

          detector.detect(mGraySubmat, mKeyPoints);
          descriptor.compute(mGraySubmat, mKeyPoints, mIntermediateMat);

        matcher.match(mIntermediateMat,descriptors,matches);
        mIntermediateMat2.create(resultSize, CvType.CV_8UC1);
        Features2d.drawMatches(img1, keypoints, mGraySubmat, mKeyPoints, matches, 
                mIntermediateMat2,GREEN, RED,  MATCH_MASK, Features2d.NOT_DRAW_SINGLE_POINTS);

          Imgproc.resize(mIntermediateMat2, mIntermediateMat2, mRgba.size());
          Imgproc.cvtColor(mIntermediateMat2, mRgba, Imgproc.COLOR_RGBA2BGRA, 4);
     Utils.matToBitmap(mRgba, bmp);

      DMatch dm[] = matches.toArray();
          List<Point> lp1 = new ArrayList<Point>(dm.length);
          List<Point> lp2 = new ArrayList<Point>(dm.length);
          KeyPoint tkp[] = keypoints.toArray();
          KeyPoint qkp[] = mKeyPoints.toArray();
          for (int i = 0; i < dm.length; i++) {
              DMatch dma = dm[i];
              lp1.add(tkp[dma.trainIdx].pt);
              lp2.add(qkp[dma.queryIdx].pt);
          }
          MatOfPoint2f pointsPrev = new MatOfPoint2f(lp1.toArray(new Point[0]));
          MatOfPoint2f pointsAct  = new MatOfPoint2f(lp2.toArray(new Point[0]));
        Log.i("pointsPrev", pointsPrev.size().toString());
        Log.i("pointsAct", pointsAct.size().toString());
          fundamental_matrix.create(resultSize, CvType.CV_8UC1);
        fundamental_matrix = Calib3d.findFundamentalMat(
                  pointsAct, pointsPrev, Calib3d.FM_RANSAC, 3, 0.99);

any suggestion?

EDIT :

i can't convert matches to list ! because Feature2d.drawMatches() need a MatOfDmatch and not a List<Dmatch>

MatOfDMatch matches, matches12, matches21;
matcher.match( descriptors1, descriptors2, matches12 );
matcher.match( descriptors2, descriptors1, matches21 );

iterate matches12
    DMatch forward = matches12[i];  
    DMatch backward = matches21[forward.trainIdx]; 
    if( backward.trainIdx == forward.queryIdx ) 
 //add forward to matches 
Features2d.drawMatches(img1, keypoints, mGraySubmat, mKeyPoints, matches,mIntermediateMat2);

解决方案

Your code should be like this:

 FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
 DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);;
 DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);

 //first image
 Mat img1 = Highgui.imread("<image1 path>");
 Mat descriptors1 = new Mat();
 MatOfKeyPoint keypoints1 = new MatOfKeyPoint();

 detector.detect(img1, keypoints1);
 descriptor.compute(img1, keypoints1, descriptors1);

 //second image
 Mat img2 = Highgui.imread("<image2 path>");
 Mat descriptors2 = new Mat();
 MatOfKeyPoint keypoints2 = new MatOfKeyPoint();

 detector.detect(img2, keypoints2);
 descriptor.compute(img2, keypoints2, descriptors2);

 //matcher should include 2 different image's descriptors
 MatOfDMatch  matches = new MatOfDMatch();             
 matcher.match(descriptors1,descriptors2,matches);
 //feature and connection colors
 Scalar RED = new Scalar(255,0,0);
 Scalar GREEN = new Scalar(0,255,0);
 //output image
 Mat outputImg = new Mat();
 MatOfByte drawnMatches = new MatOfByte();
 //this will draw all matches, works fine
 Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches, 
 outputImg, GREEN, RED,  drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);

Also if you want to display just features, you can add this code:

 Mat featuredImg = new Mat();
 Scalar kpColor = new Scalar(255,159,10);//this will be color of keypoints
 //featuredImg will be the output of first image
 Features2d.drawKeypoints(img1, keypoints1, featuredImg , kpColor, 0);
 //featuredImg will be the output of first image
 Features2d.drawKeypoints(img1, keypoints1, featuredImg , kpColor, 0);

Then you can show matched points like this:

  Bitmap imageMatched = Bitmap.createBitmap(outputImg.cols(), outputImg.rows(), Bitmap.Config.RGB_565);//need to save bitmap
  Utils.matToBitmap(outputImg, imageMatched);
  ImageView.setImageBitmap(imageMatched);

Eventually you can implement good matches. I hope this thread will be helpful.

这篇关于错误的匹配与ORB在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆