Ask Your Question
0

Accessing to OpenCv cell values of Mat from native c++ function of android app

asked 2013-09-20 09:24:35 -0600

and.ryx gravatar image

updated 2013-09-20 09:28:53 -0600

berak gravatar image

:) I'm programming an algorithm for detect symmetric radial center of an image, to detect eyes position into face framePicture. I know that already exist a project of public domain that do this work, but i would base my work about another kind of studies.

This is the scenario:

Doing by hand this manipulation frame by frame, i have seen that writting code on java layer, like this:

private Mat mGray = new Mat(height,width,CvType.CV_8U);
private Mat mOut = new Mat(height,width,CvType.CV_8U);
private Mat mIntermediateMat = Mat.zeros(height,width,CvType.CV_32F);   

[...common methods of opencv app...]

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    switch (ImageManipulationsActivity.viewMode) {
    case ImageManipulationsActivity.VIEW_MODE_RGBA:
        mOut = inputFrame.rgba();
        break;

    case ImageManipulationsActivity.VIEW_MODE_MODIFY:
        mGray = inputFrame.gray();

        int h = mGray.rows();
        int w = mGray.cols();

        int sobxVal,sobyVal;    

        /** 
        * Appling manually sobel filtering to calculate dx and dy image, 
        * moreover calculate magnitudo matrix and cosValue and sinValue
        * matrices for computing, using lut techniques.
        */
        for(int i = 1; i < h-1; i++)
            for(int j = 1; j < w-1; j++) {
                sobxVal = (int) (
                        ((int)mGray.get(i-1,j)[0] << 1) +
                        mGray.get(i-1,j-1)[0] +
                        mGray.get(i-1,j+1)[0] - (
                        ((int)mGray.get(i+1,j)[0] << 1) +
                        mGray.get(i+1,j-1)[0] +
                        mGray.get(i+1,j+1)[0] ) );
                sobyVal = (int) (
                        ((int)mGray.get(i,j-1)[0] << 1) +
                        mGray.get(i-1,j-1)[0] +
                        mGray.get(i+1,j-1)[0] - (
                        ((int)mGray.get(i,j+1)[0] << 1) +
                        mGray.get(i-1,j+1)[0] +
                        mGray.get(i+1,j+1)[0] ) );
//      compute magnitudo and atan2
            }

//      ...other calculations...

        Core.convertScaleAbs(mIntermediateMat, mOut);

    }

    return mOut;
}

is not all efficient! So I decided to write c++ for a native function to manipulate matrix in this way:

Code by c++ side

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <math.h>
#include <vector>

#include <android/log.h>

#define LOG_TAG "Example Filter"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))

#define RADIUS 10
#define _K      9.9
#define _A      2   //radial strictness parameter, found experimentally
using namespace std;

using namespace cv;

extern "C" {
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(
        JNIEnv* env, 
        jobject, 
        jlong addrGray, 
        jlong addrRgba, 
        jlong addrlutCosSin,
        jlong addrlutM );

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(
        JNIEnv* env, 
        jobject, 
        jlong addrGray, 
        jlong addrOut, 
        jlong addrlutCosSin,
        jlong addrlutM )
{
    Mat& mGr  = *(Mat*)addrGray;
    Mat& mOut = *(Mat*)addrOut;
    Mat& lutCosSin = *(Mat*)addrlutCosSin;
    Mat& lutM = *(Mat*)addrlutM;
    int w = mGr.cols;
    int h = mGr.rows;
    double sobelxVal,sobelyVal,angle;

    Mat magnitudo(h,w,CV_32F,Scalar(0));
    Mat xMat(h,w,CV_8S,Scalar(0));
    Mat yMat(h,w,CV_8S,Scalar(0));
    Mat oMat(h,w,CV_32F,Scalar(0));
    Mat mMat(h,w,CV_32F,Scalar(0));
    /*
     * Convolves Matrix with Sobel ky kernel and Sobel kx kernel
     *ky = [ 1  2  1 ;
     *       0  0  0 ;
     *      -1 -2 -1 ]
     *
     *kx = [ 1  0 -1 ;
     *       2  0 -2 ;
     *       1  0 -1 ]
     *
     * doing dedicated computation 
     */
    for( int i = 1; i < h-1; i++ )
    {
        for (int j = 1; j < w-1; j++ )
        {
            sobelxVal = (mGr.at<int>(i-1,j ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-20 09:48:40 -0600

berak gravatar image

updated 2013-09-20 10:00:13 -0600

first of all, why not use the builtin Sobel Filter ?

http://docs.opencv.org/java/org/opencv/imgproc/Imgproc.html#Sobel(org.opencv.core.Mat,%20org.opencv.core.Mat,%20int,%20int,%20int)

and then, you have to access a 1 channel uchar Mat like:

mGr.at<uchar>(i,j)    // not int !

same trouble with:

xMat.at<double>(i,j)  //   broken, must be 'char' since the the type of xMat is CV_8S
magnitudo.at<double>(i,j)  // broken, must be 'float' since the the type of magnitudo is CV_32F

cut a long story short: the type whithin the braces has to fit the Mat's type, you can't choose it at will.

edit flag offensive delete link more

Comments

Thank you bery much. I know that exist Sobel function that apply sobel filter to the image but, i have to do moreover than this application. So i just want to try manual filtering with a simple convolution! However i thank you again :-D !!

and.ryx gravatar imageand.ryx ( 2013-10-02 05:10:34 -0600 )edit

hmmm, still, do care to get the types right here. maybe even, instead of

Mat magnitudo(h,w,CV_32F,Scalar(0));

you could try to use:

Mat_<float> magnitudo(h,w,Scalar(0));

and access its elements like:

magnitudo(i,j);

this way, you have to think about the type only once, and get rid of the .at<type>(i,j)

berak gravatar imageberak ( 2013-10-02 05:41:07 -0600 )edit

Question Tools

Stats

Asked: 2013-09-20 09:24:35 -0600

Seen: 1,851 times

Last updated: Sep 20 '13