I am using OpenCV4Android for a project which involves both Java as well as C++ code. I am finding keypoints in an image using FAST feature detector using OpenCV's Java API. I need to pass its output (set of keypoints) which is a MatofKeypoint object in Java to a native C++ method. In the C++ method I need to use it as vector so that I can extract Keypoint descriptors form it.
I passed MatofObject form java and received it as a Mat& in C++, then manually tried to convert Mat& to vector by manually reading each point as described here. But the program crashes every time I access the received Mat& object with fatal signal 11,code 1.
I suspect the problem is due to difference in data structures used by C++ and Java API.
Any help will be appreciated, Thankyou!!!
In Java
public native void processImage(long matAddrTemplateKeypoints); // Native Method definition
FeatureDetector detector = FeatureDetector.create(FeatureDetector.FAST);
MatOfKeyPoint templateKeypoints = new MatOfKeyPoint();
detector.detect(img1, templateKeypoints);
processImage(templateKeypoints.getNativeObjAddr()); // Native method Call
In C++
JNIEXPORT void JNICALL MainActivity_processImage(JNIEnv*, jobject, jlong matAddrTemplateKeypoints)
Mat& templateKeypointMat = *(Mat*) matAddrTemplateKeypoints; // Casting received MatofPoint object as a Mat&
for(int i=0;i<templateKeypointMat.rows; i++){ // Code crashes here
...
}
an answer was posted in the opencv-internal forum: http://answers.opencv.org/question/30869/how-to-pass-a-matofkeypoint-and-matofpoint2f-to-native-code-opencv-4-android/
Here is the code for KeyPoint and DMatch conversion:
// C++ / JNI
// vector_KeyPoint converters
using namespace cv;
using namespace std;
void Mat_to_vector_KeyPoint(Mat& mat, vector<KeyPoint>& v_kp)
{
v_kp.clear();
assert(mat.type()==CV_32FC(7) && mat.cols==1);
for(int i=0; i<mat.rows; i++)
{
Vec<float, 7> v = mat.at< Vec<float, 7> >(i, 0);
KeyPoint kp(v[0], v[1], v[2], v[3], v[4], (int)v[5], (int)v[6]);
v_kp.push_back(kp);
}
return;
}
void Mat_to_vector_DMatch(Mat& mat, vector<DMatch>& v_dm)
{
v_dm.clear();
assert(mat.type()==CV_32FC(4) && mat.cols==1);
for(int i=0; i<mat.rows; i++)
{
Vec<float, 4> v = mat.at< Vec<float, 4> >(i, 0);
DMatch dm((int)v[0], (int)v[1], (int)v[2], v[3]);
v_dm.push_back(dm);
}
return;
}
void Vector_KeyPoint_to_Mat(vector<KeyPoint>& v_kp, Mat& mat)
{
int count = (int)v_kp.size();
mat.create(count, 1, CV_32FC(7));
for(int i=0; i<count; i++)
{
KeyPoint kp = v_kp[i];
mat.at< Vec<float, 7> >(i, 0) = Vec<float, 7>(kp.pt.x, kp.pt.y, kp.size, kp.angle, kp.response, (float)kp.octave, (float)kp.class_id);
}
}
void Vector_DMatch_to_Mat(vector<DMatch>& v_dm, Mat& mat)
{
int count = (int)v_dm.size();
mat.create(count, 1, CV_32FC(4));
for(int i=0; i<count; i++)
{
DMatch dm = v_dm[i];
mat.at< Vec<float, 4> >(i, 0) = Vec<float, 4>((float)dm.queryIdx, (float)dm.trainIdx, (float)dm.imgIdx, dm.distance);
}
}
I just tried it on some examples and it worked for me very well.