Ask Your Question

peanutman's profile - activity

2020-11-05 14:06:20 -0500 received badge  Good Question (source)
2020-10-05 04:19:33 -0500 received badge  Notable Question (source)
2017-10-14 20:32:53 -0500 received badge  Popular Question (source)
2017-03-07 12:19:30 -0500 received badge  Popular Question (source)
2016-09-19 13:57:42 -0500 received badge  Famous Question (source)
2015-06-09 06:03:35 -0500 received badge  Nice Question (source)
2015-01-29 10:45:56 -0500 received badge  Nice Question (source)
2015-01-26 09:55:59 -0500 received badge  Notable Question (source)
2014-06-25 04:00:17 -0500 received badge  Popular Question (source)
2013-04-24 14:09:24 -0500 received badge  Student (source)
2013-04-24 03:30:28 -0500 asked a question Can the Java interface pass a Mat to OpenCV's C interface via JNI


I am writing a desktop Java application that uses the new Java interface to OpenCV. However, because of certain Java limitations I have to use JNI to capture the images from the hardware. This is done by creating an empty Mat on the Java side, and using getNativeObjAddr() to pass a pointer to the native side. On the native side the Mat is assigned the proper size and filled with the image data.

In the android example (tutorial 2) they use C++, and do something like this:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
extern "C" {
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);

JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
    Mat& mGr  = *(Mat*)addrGray;
    Mat& mRgb = *(Mat*)addrRgba;
    ... do stuff with the Mat objects ...

As you can see they make use of the C++ interface (extern C is used for linking purposes). However, so far I've been building the JNI side in plain C. My question is, can I use OpenCV's C interface to modify the Mat? I'm guessing the getNativeObjAddr method returns a pointer to the C++ class instance, and not a C struct. I'm also not sure on how the cast would differ from the C++ version.

Anyone here with JNI experience that would know this?

Thanks for your time!

2013-04-23 06:44:42 -0500 received badge  Supporter (source)
2013-04-23 06:43:34 -0500 commented answer Returning a Mat from native JNI to Java

Hah, I feel stupid now... I like this approach, as it doesn't require the bridge to be crossed once we're in the function. Thanks for that!

2013-04-23 06:39:33 -0500 received badge  Scholar (source)
2013-04-22 05:42:27 -0500 received badge  Editor (source)
2013-04-22 05:40:18 -0500 asked a question Returning a Mat from native JNI to Java

I have a question about mixing OpenCV's C++ and Java API. I'm using the new desktop Java API to do some processing on images, but due to Java limitations I am forced to capture the images in C++. The images are in an OS-dependant format, so I thought it would be best to convert them to the OpenCV format before handing it to the OS-independant Java code. In the android tutorials (Tutorial 2 Advanced) I saw that they instantiate a Mat on the Java side, and use getNativeObjAddr() to pass a pointer to C++, where it is used as a native C++ object. However, since I can not make any assumptions on the dimensions or channels of the images, I can not take this approach. I want Java to be able to receive the Mat object without knowing anything about it beforehand. I have nu clue how to do this correctly. I have two ideas:

1) I create a Java Mat object from the C++ side ((*env)->NewObject), use JNI to call getNativeObjAddr and work with the returned pointer. While this might work, it seems so backwards... Maybe there's a better way?

2) I create a C++ Mat, and return the pointer to Java. I'm hoping there's some kind of functionality that allows me to wrap it in a Java Mat object. The documentation mentions the Mat(long addr) constructor, but there's no further explanation, and I have no idea what it's for. Can it be used to create a Mat object from a pointer to a native Mat object?

TL;DR: How do I get a Mat created in C++ to Java ?

Thank you for reading!

2013-01-05 05:36:31 -0500 asked a question Efficient difference of gaussians


I am trying to implement difference of guassians (DoG), for a specific case of edge detection. As the name of the algorithm suggests, it is actually fairly straightforward:

Mat g1, g2, result;
Mat img = imread("test.png", CV_LOAD_IMAGE_COLOR); 
GaussianBlur(img, g1, Size(1,1), 0);
GaussianBlur(img, g2, Size(3,3), 0);
result = g1 - g2;

However, I have the feeling that this can be done more efficiently. Can it perhaps be done in less passes over the data? I have encountered the concept of seperable filters, but I'm too much of an image processing noob to understand how to apply them in this case.