Ask Your Question

adhamenaya's profile - activity

2019-05-25 14:52:28 -0500 received badge  Popular Question (source)
2015-10-19 10:10:41 -0500 commented answer Assigning OpenCV Mat object causes a memory leak

What I don't understand "C++ class that is used by JNI to make it a Java class" .. What do you mean in to make it a Java class ?!

2015-10-18 14:20:45 -0500 asked a question Assigning OpenCV Mat object causes a memory leak

I want to assign frame (Mat object) from function parameter into object variable, as shown in the code below. But this function should be called may times (for each frame from the video camera). but this line

this->nFrame = frame;

causes a memory leak (when commented there is no error!).

NOTE The function setCurrentFrame is called inside JNI function, where this JNI function is called every time I want to process the frame from the video camera.

The JNI function is like:

JNIEXPORT jbyteArray JNICALL Java_com_adhamenaya_Native_run(JNIEnv * env,
        jobject obj, jstring faceCascadeFile, jstring noseCascadeFile,
        jstring landmarks, jlong frame) {

    MyClass gsys;
    cv::Mat& inFrame = *(cv::Mat*) frame;
    gsys.setCurrentFrame(inFrame);


    // SOME PROCESSING FOR THE FRAME 


    inFrame.release();
    gsys.release();

    ......
    ......
}

The code for C++ function (setCurrentFrame)

void MyClass::setCurrentFrame(cv::Mat& frame) {
    cv::Size2d imgRes;
    float resRatio;

    if (frame.cols > frame.rows) {
        //landscape
        imgRes.width = 640.0f;
        resRatio = frame.cols / 640.0f;
        imgRes.height = floor(frame.rows / resRatio);
    } else {
        //portrait
        imgRes.height = 640.0f;
        resRatio = frame.rows / 640.0f;
        imgRes.width = floor(frame.cols / resRatio);
    }

    //save scaled height, width for further use
    this->frameWidth = nFrame.cols;
    this->frameHeight = nFrame.rows;

    //set frame and increment frameCount
    this->nFrame = frame;
    this->frameCount++;
}

Kindly, can you help me to solve this problem ? I tried to release the frame by calling :

void MyClass::release(void) {
    this->nFrame = cv::Mat();
}

nothing happened, even like this:

void MyClass::release(void) {
    this->nFrame.release();
}

Still the same error!

2015-10-11 05:23:04 -0500 received badge  Enthusiast
2015-10-10 15:31:08 -0500 received badge  Supporter (source)
2015-10-10 05:51:09 -0500 asked a question Error: “Fatal signal 11 (SIGSEGV), code 1” when passing Mat object from java to jni function

I am running the video camera using the OpenCV function. I pass the Mat object to the jni function it works for awhile, them the error:

package com.adhamenaya;

import java.util.ArrayList;

import org.opencv.android.BaseLoaderCallback; import org.opencv.android.CameraBridgeViewBase; import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame; import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2; import org.opencv.android.LoaderCallbackInterface; import org.opencv.android.OpenCVLoader; import org.opencv.core.Mat; //import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc;

import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.View.OnTouchListener; import android.view.WindowManager;

public class MainActivity extends Activity implements CvCameraViewListener2,
        OnTouchListener {

    private static final String TAG = "OCVSample::Activity";
    private Mat mRgba;
    private Mat mGray;
    private CameraBridgeViewBase mOpenCvCameraView;

    private ArrayList<Mat> mats = new ArrayList<Mat>();

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
            case LoaderCallbackInterface.SUCCESS: {
                Log.i(TAG, "OpenCV loaded successfully");
                mOpenCvCameraView.enableView();
                mOpenCvCameraView.setOnTouchListener(MainActivity.this);

            }
                break;
            default: {
                super.onManagerConnected(status);
            }
                break;
            }
        }
    };

    public MainActivity() {
        Log.i(TAG, "Instantiated new " + this.getClass());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "called onCreate");
        super.onCreate(savedInstanceState);

        Native.loadlibs();


        mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.cam_view);
        mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
        mOpenCvCameraView.setCvCameraViewListener(this);

        Native.setup(mFaceCascadeFile, mNoseCascadeFile, mLandmarks);
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

    @Override
    public void onResume() {
        super.onResume();
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this,
                mLoaderCallback);
    }

    public void onDestroy() {
        super.onDestroy();
        if (mOpenCvCameraView != null)
            mOpenCvCameraView.disableView();
    }

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

    public void onCameraViewStopped() {
    }

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        mRgba = inputFrame.rgba();
        Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_BGRA2GRAY);
        Native.runJni(mFaceCascadeFile, mNoseCascadeFile, mLandmarks,
                mRgba.getNativeObjAddr());

        return mRgba;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return false;
    }


}

Jni function:

JNIEXPORT jbyteArray JNICALL Java_com_adhamenaya_Native_runJni(JNIEnv * env, jobject obj, jstring faceCascadeFile, jstring noseCascadeFile, jstring landmarks, jlong frame) {

cv::Mat& inFrame = *(cv::Mat*) frame;

if (!gsys.loadFaceCascade(faceCascadeFnameStr)) {
    LOG("Could not load face cascade");
    gsys.loadFaceCascade(faceCascadeFnameStr);
} else {
    LOG("Face cascade: OK");

}

if (!gsys.loadNoseCascade(noseCascadeFnameStr)) {
    LOG("Could not load nose cascade");
    gsys.loadNoseCascade(noseCascadeFnameStr);

} else {
    LOG("Nose cascade: OK");

}

gsys.setFrameRate(30);
gsys.setProgramState(DETECT);

clock_t tin, tout = 0;

cv::flip(inFrame, inFrame, 0);
cv::transpose(inFrame, inFrame);

dlib::shape_predictor pose_model;
dlib::deserialize(landmarksStr) >> pose_model;


 gsys.setCurrentFrame(inFrame);

 tin = clock();
 trigger_hr(gsys, faces, pose_model);

 // Process the frame
 size_t spm;
 float motionStrengthX, motionStrengthY;
 float phiYaw = -0xFFFFFFFF, thetaPitch = -0xFFFFFFFF;

 if (faces.size()) {
 faces[0].getSpm(gsys, spm, motionStrengthX, motionStrengthY);
 faces[0].getFacePose(phiYaw, thetaPitch);
 }

 tout = tout + clock() - tin;
 if ((gsys.getFrameCount() % 30) == 29) {
 double secs_between_frames = (double) (tout) / (CLOCKS_PER_SEC * 30.0f);
 printf("FPS = %2.2f\n", 1.0f / secs_between_frames);
 LOG("FPS = %2.2f ", 1.0f / secs_between_frames);
 tout = 0;
 }

 char spmText[100];

 //sprintf(spmText,
 //     "SPM = %zu, P = %2.2f, T = %2.2f, MS-X = %2.2f, MS-Y = %2.2f", spm,
 //     phiYaw, thetaPitch, motionStrengthX, motionStrengthY);

 LOG("SPM = %zu, P = %2.2f, T = %2 ...
(more)
2015-08-15 11:57:51 -0500 asked a question OpenCV4Android 3.0: VideoCapture doesn't open

Hi,

I want to open the camera from the native code using the OpenCV SDK for android (native) version 3. I am trying using android Nexus 5 android 5.0.1

My android.mk file:

LOCAL_PATH := $(call my-dir)

OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on

include $(CLEAR_VARS)
include D:\folder1\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk
LOCAL_C_INCLUDES := D:\folder1\OpenCV-android-sdk\sdk\native\jni\include

OPENGLES_LIB := -lGLESv1_CM
OPENGLES_DEF := -DUSE_OPENGL_ES_1_1

LOCAL_LDLIBS += -lGLESv1_CM -ldl -llog
LOCAL_MODULE  := NativeCamera

LOCAL_SRC_FILES := Camera.cpp
include $(BUILD_SHARED_LIBRARY)

The c++ code :

#include <jni.h>
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <android/log.h>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h>
#include <pthread.h>
#include <time.h>
#include <Math.h>

......
......
JNIEXPORT void JNICALL Java_com_example_opencvnativecamera_Native_initCamera(JNIEnv*, jobject,jint width,jint height)

{
 LOG("Camera Created");
 capture.open(CV_CAP_ANDROID + 0);
 capture.set(CV_CAP_PROP_FRAME_WIDTH, width);
 capture.set(CV_CAP_PROP_FRAME_HEIGHT, height);
......
}

Could you please help me in figuring out what is the problem ? libraries are correct ? Are there any libs missed ?

Thanks,