opencv在Android上实现物体跟踪(7)简介
实现步骤
原理介绍
matchTemplate介绍
代码实现
JNIEXPORT void Java_com_example_camera_1opencv_1android_PreviceGray_grayProc(JNIEnv* env, jclass obj, jlong imageGray,jint touch_x,jint to  int width,height;
int k = 0,n = 0,i,j;
CvScalar s;
int result_cols;
int result_rows;
Mat result;
Mat mat = Mat(*((Mat*)imageGray));
width = ws;
height = ls;
result_cols = height - Template + 1;
result_rows = width - Template + 1;
double minVal;
double maxVal;
Point minLoc;
Point maxLoc;
Point matchLoc;
int match_method = CV_TM_SQDIFF;
cv::Mat img = cv::Mat(Template,Template,CV_8UC3,cv::Scalar(0,0,0));
IplImage pI = mat;
IplImage pI_2 = img;
Mat img_display = cv::Mat(width,height,CV_8UC3,cv::Scalar(0,0,0));
IplImage pI_3 = img_display;
for(i=0;i<width;i++){
for(j=0;j<height;j++){
s = cvGet2D(&pI,i,j);
cvSet2D(&pI_3,i,j,s);
}
rectangle函数opencv
}
for(i=(touch_x-(Template/2));i<(touch_x+(Template/2));i++){
for(j=(touch_y-(Template/2));j<(touch_y+(Template/2));j++){
s = cvGet2D(&pI,j,i);
cvSet2D(&pI_2,k,n,s);
n++;
}
k++;
n=0;
}
/
// Do the Matching and Normalize
matchTemplate( img_display, img, result, match_method);
normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat() );
/// Localizing the best match with minMaxLoc
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if(match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED ){
matchLoc = minLoc;
}else{
matchLoc = maxLoc;
}
rectangle(mat, matchLoc, Point(matchLoc.x + ls , matchLoc.y + ws ), Scalar::all(0), 2, 8, 0);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。