Ask Your Question

Converting .Mat to CvArr format (JavaCV)

asked 2013-04-08 01:15:13 -0500

NightLife gravatar image

updated 2013-04-08 03:45:34 -0500

I wnat to use cvRectangle() in Javacv and the first parameter is the current frame of camera. But the problem is that format of the frames are .Mat and I need to convert them to CvArr format to use on this command for my android application (Template Matching android-OpenCv application).

How can I do the conversion?

Please help me with that.


her is my code:

public void overlapFinder( String inputPath )//inputhpath is the template image path to load

//mRgba is the current frame in Mat format

    // to find the best overlap in the current frame with template image

    CvPoint minloc = new CvPoint(0,0) ;
    CvPoint maxloc = new CvPoint(0,0);

    double minval, maxval;

    Mat     mResult; 

    mTemplate = Highgui.imread(inputPath);

    int resultWidth =  mRgba.width() - mTemplate.width() + 1; 

    int resultHeight = mRgba.height() - mTemplate.height() + 1;

    mResult = new Mat (resultHeight, resultWidth, CvType.CV_32F);

    //cvMinMaxLoc( mResult, minval, maxval, minloc, maxloc, 0 );

    IplImage destImage = mRgba;

    cvRectangle(mRgba, cvPoint( minloc.x(), minloc.y()),
            cvPoint( minloc.x() + mTemplate.width(), minloc.y() + mTemplate.height()),
            cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );        

    Imgproc.matchTemplate(mRgba, mTemplate , mResult , Imgproc.TM_CCOEFF) ; 

    Core.MinMaxLocResult result = Core.minMaxLoc(mResult);

    double maxVal = result.maxVal;
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-04-08 03:09:55 -0500

Basically you need a type conversion from the Mat element towards the older IplImage structure, which is a cvArr structure if I recall correctly.

Mat image;
IplImage tmp = image;

This works in C++ style, can you try if it works in Java?

edit flag offensive delete link more


Actually, this method does not usfull for Java. It makes error. Do you have another Idea? Or do you know way to use cvRectangle from openCV4Android library instead?

NightLife gravatar imageNightLife ( 2013-04-08 03:21:26 -0500 )edit

Could you add your code to the original post, I will have a look at it :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-08 03:29:07 -0500 )edit

I have edited the question and added the code to it .

thanks in advance :)

NightLife gravatar imageNightLife ( 2013-04-08 03:43:28 -0500 )edit

And where exactly is the error arising? On the first look, this seems correct.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-08 03:56:57 -0500 )edit

You are right, on the first look it is correct but the eclipse makes error for the "CvRectangle" and also "cvMinMaxLoc" because of input frame/image. It should not be in Mat format !!! The acceptable format is CvArr! (It is exactly the error) On the other hand, "Imgproc.matchTemplate" use the mat format as an input!!

NightLife gravatar imageNightLife ( 2013-04-08 04:06:02 -0500 )edit

You are inserting a 4 channel image RGBA into an cvRectangle. I think that it can only handle 1 channel (grayscale) and 3 channel (color BGR) images. Try to remove the 4th channel for a minute and see what happens.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-08 04:35:06 -0500 )edit

It is strange, still does not work!! It is the error :

The method cvRectangle(opencv_core.CvArr, opencv_core.CvPoint, opencv_core.CvPoint, opencv_core.CvScalar, int, int, int) in the type opencv_core is not applicable for the arguments (Mat, opencv_core.CvPoint, opencv_core.CvPoint, opencv_core.CvScalar, int, int, int)

NightLife gravatar imageNightLife ( 2013-04-08 04:47:36 -0500 )edit

I am guessing this could be a bug, where a wrong type was defined in the java wrapper for this class. @Daniil Osokin, I have no experience with bug reporting, could you maybe point this guy to the correct place?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-08 04:50:26 -0500 )edit

Btw I just checked the Java docs for openCV, it seems that there is actually no cvRectangle class supported in current build, but they use the Rect functionality instead. Look at this link : then go to all classes --> Rect

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-08 04:52:40 -0500 )edit

BTW Daniil, is it normal that a function is supported in JavaCV without it being available in the docs? I guess he is mixing up C++ and Java code here?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-08 05:44:16 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-04-08 01:15:13 -0500

Seen: 4,577 times

Last updated: Apr 08 '13