Android OpenCV - 对象跟踪

时间:2013-12-20 13:09:28

标签: android opencv

我使用功能检测实现对象跟踪,我收到以下错误:

  

12-19 20:59:16.943:E / cv :: error()(31858):OpenCV错误:断言失败(src.dims == 2&& info.height ==(uint32_t)src。 rows&& info.width ==(uint32_t)src.cols)in void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv *,jclass,jlong​​,jobject,jboolean),file / home / reports / ci / slave_desktop / 50-SDK / opencv / modules /java/generator/src/cpp/utils.cpp,第97行   12-19 20:59:16.943:E / org.opencv.android.Utils(31858):nMatToBitmap catched cv :: Exception:/ home / reports / ci / slave_desktop / 50-SDK / opencv / modules / java / generator / src / cpp / utils.cpp:97:错误:(-215)src.dims == 2&& info.height ==(uint32_t)src.rows&& info.width ==(uint32_t)函数中的src.cols void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv *,jclass,jlong​​,jobject,jboolean)   12-19 20:59:16.943:E / CameraBridge(31858):Mat类型:Mat [820 * 2672 * CV_8UC3,isCont = true,isSubmat = false,nativeObj = 0x55650380,dataAddr = 0x5ec20010]   12-19 20:59:16.943:E / CameraBridge(31858):位图类型:960 * 720   12-19 20:59:16.943:E / CameraBridge(31858):Utils.matToBitmap()抛出异常:/ home / reports / ci / slave_desktop / 50-SDK / opencv / modules / java / generator / src / cpp / utils.cpp:97:错误:(-215)src.dims == 2&& info.height ==(uint32_t)src.rows&& info.width ==(uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv *,jclass,jlong​​,jobject,jboolean)

这是代码:

    public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat();
    mGray = new Mat();
    mView = new Mat();
    mObject = new Mat();
}

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

    switch (viewMode) {
    case VIEW_MODE_RGBA:

        return mRgba;

    case VIEW_MODE_FeatureDetect:
        try {
        mGray = inputFrame.gray();
        mObject = new Mat();
        mObject = Highgui.imread(Environment.getExternalStorageDirectory()+ "/Android/data/" + getApplicationContext().getPackageName() + "/Files/Object.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
        mView = mGray.clone();          

        FeatureDetector myFeatureDetector = FeatureDetector.create(FeatureDetector.ORB);

        MatOfKeyPoint keypoints = new MatOfKeyPoint();
        myFeatureDetector.detect(mGray, keypoints);

        MatOfKeyPoint objectkeypoints = new MatOfKeyPoint();
        myFeatureDetector.detect(mObject, objectkeypoints);

        DescriptorExtractor Extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
        Mat sourceDescriptors = new Mat();
        Mat objectDescriptors = new Mat();
        Extractor.compute(mGray, keypoints, sourceDescriptors);
        Extractor.compute(mGray, objectkeypoints, objectDescriptors);
        DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);

        MatOfDMatch matches = new MatOfDMatch();
        matcher.match(sourceDescriptors, objectDescriptors, matches);

        Features2d.drawMatches(mGray, keypoints, mObject, objectkeypoints, matches, mView);

        return mView;
        } catch (Exception e) {
            Log.d("Exception",e.getMessage());
        }

    }

    return mRgba;
}

它无法在屏幕上显示对象,但我需要一些信息,我可以放置矩形或者我可以将对象标记为我想要跟踪的内容。 抱歉我的英语,我希望你明白我在问什么,谢谢你的任何建议。

1 个答案:

答案 0 :(得分:1)

感谢Darshan的建议,我解决了问题,需要调整图片大小, 像Features2d.drawMatches(mGray, keypoints, mObject, objectkeypoints, matches, mView);一样在Imgproc.resize(mView, mView, mGray.size());下方添加此代码,然后它就可以正常运行。