如果检测到对象,ORB保存视频帧

时间:2016-07-02 16:26:51

标签: android algorithm opencv brute-force orb

我正在开发一款Android应用,它使用ORB算法查找从相机接收的视频流中的对象。我要做的是保存对象出现的视频帧。我不知道我做错了什么,但我保存了所有视频帧,即使对象不在图像中。这是我的代码:

//partea de feature description si matching
    int maximumNuberOfMatches=10;
    Mat greyInputImage=new Mat();
    Mat frameToMatch=cameraFrameRgba;

    Imgproc.cvtColor(mInputImage, greyInputImage, Imgproc.COLOR_RGB2GRAY);
    Imgproc.cvtColor(frameToMatch, cameraFrameGray, Imgproc.COLOR_RGB2GRAY);
    MatOfKeyPoint keyPoints=new MatOfKeyPoint();
    MatOfKeyPoint keyPointsToMatch=new MatOfKeyPoint();

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
    detector.detect(greyInputImage, keyPoints);
    Features2d.drawKeypoints(greyInputImage, keyPoints, greyInputImage);
    // displayImage(greyImage);

    detector.detect(greyInputImage, keyPoints);
    detector.detect(cameraFrameGray, keyPointsToMatch);

    DescriptorExtractor dExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);

    Mat descriptors=new Mat();
    Mat descriptorsToMatch=new Mat();

    dExtractor.compute(greyInputImage, keyPoints, descriptors);
    dExtractor.compute(cameraFrameGray, keyPointsToMatch, descriptorsToMatch);
 DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
    MatOfDMatch matches=new MatOfDMatch();
    matcher.match(descriptorsToMatch,descriptors,matches);
    ArrayList<DMatch> goodMatches=new ArrayList<DMatch>();
    List<DMatch> allMatches=matches.toList();

    double minDist = 100;
    for( int i = 0; i < descriptorsToMatch.rows(); i++ )
    {
        double dist = allMatches.get(i).distance;
        if( dist < minDist ) minDist = dist;
    }
    for( int i = 0; i < descriptorsToMatch.rows() && goodMatches.size()
            <maximumNuberOfMatches; i++ )
    {
        if(allMatches.get(i).distance<= 2*minDist)
        {
            goodMatches.add(allMatches.get(i));
        }
    }
    MatOfDMatch goodEnough=new MatOfDMatch();
    goodEnough.fromList(goodMatches);
    Mat finalImg=new Mat();
    Features2d.drawMatches(cameraFrameGray, keyPointsToMatch, mInputImage, keyPoints, goodEnough, finalImg, Scalar.all(-1),Scalar.all(-1),new MatOfByte(), Features2d.DRAW_RICH_KEYPOINTS + Features2d.NOT_DRAW_SINGLE_POINTS);

    int w = 1920, h = 1080;

    Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
    Bitmap imageToStore = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap

    Utils.matToBitmap(cameraFrameGray,imageToStore);

我已经包含了匹配部分,因为我认为存在我正在犯错误的部分。 Bitmap imageToStore是我想要保存视频帧的图像。

请帮我找到问题。

0 个答案:

没有答案