参考Sample:facedetection,本机代码如下:
inline void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
{
mat = Mat(v_rect, true);
}
JNIEXPORT void JNICALL Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect
(JNIEnv * jenv, jclass, jlong thiz, jlong imageGray, jlong faces)
{
LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect enter");
try
{
vector<Rect> RectFaces;
((DetectionBasedTracker*)thiz)->process(*((Mat*)imageGray));
((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
for(int i=0; i<RectFaces.size(); i++)
{
Point p,k;
p.x = ((Rect)RectFaces[i]).x;
p.y = ((Rect)RectFaces[i]).y;
k.x = ((Rect)RectFaces[i]).x + ((Rect)RectFaces[i]).width;
k.y = ((Rect)RectFaces[i]).y + ((Rect)RectFaces[i]).height;
rectangle(*((Mat*)imageGray), p, k,Scalar(0,255,255, 255), -1, 8);
}
vector_Rect_to_Mat(RectFaces, *((Mat*)faces));
}
catch(cv::Exception& e)
{
LOGD("nativeCreateObject caught cv::Exception: %s", e.what());
jclass je = jenv->FindClass("org/opencv/core/CvException");
if(!je)
je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, e.what());
}
catch (...)
{
LOGD("nativeDetect caught unknown exception");
jclass je = jenv->FindClass("java/lang/Exception");
jenv->ThrowNew(je, "Unknown exception in JNI code{highgui::VideoCapture_n_1VideoCapture__()}");
}
LOGD("Java_org_opencv_samples_facedetect_DetectionBasedTracker_nativeDetect exit");
}
现在,提取的(多个)ROI将位于向量上的Rect容器中,然后传递给名为“faces”的Java类型MatOfRect。假设问题是通过JNI通过地址传递的此Java类型的反向转换,以将ROI从“面”重新提取到向量roi。