convert bitmap to Mat with new image and return it in opencv android

asked 2016-02-25 01:56:53 -0600

Tashen Jazbi gravatar image

I'm new with opencv in android. I've downloaded latest opencv sdk for gradle android studio. I'v imported its sample for face detection and successfully build it. it looks like that image description

it is drawing green rectangle around the face. I want to get the area inside that rectangle, replace it with a new image from drawable folder by using bitmap. What i've done right now to achieve my goal is here

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

        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);
        }
        else {
            Log.e(TAG, "Detection method is not selected!");
        }

        Rect[] facesArray = faces.toArray();
        for (int i = 0; i < facesArray.length; i++)
            Imgproc.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
        try {

             resultBitmap = BitmapFactory.decodeResource(getResources(),
                     R.drawable.icon);

           Utils.bitmapToMat(resultBitmap, mRgba);
        }
            catch(Exception e){
                Log.e(TAG, "Length of resultBitmap" + resultBitmap.getByteCount());
                resultBitmap.recycle();
                resultBitmap=null;

            }

            return mRgba;
    }

What i've done so far is: i'm getting the Mat object, create a bitmap with an image from drawable folder,convert that bitmap to Mat object again, and return that Mat object. But unfortunately this doesn't display anything. Can anybody help me that where i'm doing wrong. Any help would be much appreciated. Thank you :)

edit retag flag offensive close merge delete