JNI OpenCV Android绘制功能线或矩形

时间:2014-01-02 03:37:42

标签: android c++ opencv android-ndk java-native-interface

我在使用OpenCV的android jni app中绘制函数时遇到了很大问题。我在我的原生函数中跟踪对象并将矩形Vector by Mat转发到Java代码,然后在Java中使用RGBA Mat绘制矩形,这一切都有效。但是现在,我在返回Java之前在我的函数中用本机代码绘制了这些矩形,但它不起作用。我试图从jni中绘制任何东西,但仍然没有效果。绘制的函数不会改变Mat,后者变成Java。我非常感谢你的帮助。

这是我的原生代码:

JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect
(JNIEnv * jenv, jclass, jlong thiz, jlong imageGray, jlong faces)
{
    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect enter");
    try
    {
        vector<Rect> RectFaces;
        ((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));
        ((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
        for(int i=0; i<RectFaces.size(); i++)
        {
            Point p,k;
            p.x = ((Rect)RectFaces[i]).x;
            p.y = ((Rect)RectFaces[i]).y;
            k.x = ((Rect)RectFaces[i]).x + ((Rect)RectFaces[i]).width;
            k.y = ((Rect)RectFaces[i]).y + ((Rect)RectFaces[i]).height;
            rectangle(*((Mat*)imageGray), p, k,Scalar(0,255,255, 255), -1, 8);
        }
        vector_Rect_to_Mat(RectFaces, *((Mat*)faces));
    }
    catch(cv::Exception& e)
    {
        LOGD("nativeCreateObject caught cv::Exception: %s", e.what());
        jclass je = jenv->FindClass("org/opencv/core/CvException");
        if(!je)
            je = jenv->FindClass("java/lang/Exception");
        jenv->ThrowNew(je, e.what());
    }
    catch (...)
    {
        LOGD("nativeDetect caught unknown exception");
        jclass je = jenv->FindClass("java/lang/Exception");
        jenv->ThrowNew(je, "Unknown exception in JNI code {highgui::VideoCapture_n_1VideoCapture__()}");
    }
    LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect exit");
}

和返回rgba Mat的java代码:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

        mRgba = inputFrame.rgba();
        mGray = inputFrame.gray();
        //Imgproc.Canny(mGray, mCanny, CANNY_MIN_TRESHOLD, CANNY_MAX_TRESHOLD);
        Point center = new Point(mRgba.width() / 2, mRgba.height() / 2);
        //must be 1 channels 8 bit!
        /*//do Hough transform to find lines
        double rho = 1;
        double theta = Math.PI/180;
        Imgproc.HoughLinesP(mCanny, mLines, rho, theta, HOUGH_TRESHOLD, HOUGH_MIN_LINE_LENGTH, HOUGH_MAX_LINE_GAP);*/

        if (mAbsoluteFaceSize == 0) {
            int height = mGray.rows();
            if (Math.round(height * mRelativeFaceSize) > 0) {
                mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
            }
            mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
        }

        MatOfRect faces = new MatOfRect();

        if (mDetectorType == JAVA_DETECTOR) {
            if (mJavaDetector != null)
                mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2,
                        2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                        new Size(mAbsoluteFaceSize, mAbsoluteFaceSize),
                        new Size());
        } else if (mDetectorType == NATIVE_DETECTOR) {
            if (mNativeDetector != null)
                mNativeDetector.detect(mGray, faces);
                //mNativeDetector.findLines(mGray, mCanny);
        } else {
            Log.e(TAG, "Detection method is not selected!");
        }

        /*Rect[] facesArray = faces.toArray();
        for (int i = 0; i < facesArray.length; i++) {
            if (center.x > facesArray[i].tl().x
                    && center.x < facesArray[i].br().x) {
                if (center.y > facesArray[i].tl().y
                        && center.y < facesArray[i].br().y) {
                    Core.rectangle(mRgba, facesArray[i].tl(),
                            facesArray[i].br(), CAR_RECT_COLOR_RED, 3);
                } else {
                    Core.rectangle(mRgba, facesArray[i].tl(),
                            facesArray[i].br(), CAR_RECT_COLOR_YELLOW, 3);
                }
            } else {
                Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(),
                        CAR_RECT_COLOR_YELLOW, 3);
            }
        }*/

        Point p1 = new Point(mRgba.width() / 2, 0);
        Point p2 = new Point(mRgba.width() / 2, mRgba.height());

        Point p3 = new Point(0, mRgba.height() / 2);
        Point p4 = new Point(mRgba.width(), mRgba.height() / 2);

        Core.line(mRgba, p3, p4, AXIS_COLOR);
        Core.line(mRgba, p1, p2, AXIS_COLOR);


        return mRgba;
        //return mLines;
    }

1 个答案:

答案 0 :(得分:0)

为什么要将灰度图像传递给函数? 如果要在屏幕上显示内容,则应传递RGBA数据。