OpenCV 模版匹配

2018-05-03  本文已影响0人  守候_小7爺

Mat mCameraMat = inputFrame.rgba();

// Create the result matrix

int result_cols = mCameraMat.cols() - mTemplateMat.cols() + 1;

int result_rows = mCameraMat.rows() - mTemplateMat.rows() + 1;

Log.d(TAG, " mCameraMat cols " + mCameraMat.cols());

Log.d(TAG, " mCameraMat rows " + mCameraMat.rows());

Log.d(TAG, " mTemplateMat cols " + mTemplateMat.cols());

Log.d(TAG, " mTemplateMat rows " + mTemplateMat.rows());

Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

          // Do the Matching and Normalize

Imgproc.matchTemplate(mCameraMat, mTemplateMat, result, match_method);

Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

          // Localizing the best match with minMaxLoc

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

Point matchLoc;

if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {

    matchLoc = mmr.minLoc;

  } else {

  matchLoc = mmr.maxLoc;

}

Rect roi = new Rect((int) matchLoc.x, (int) matchLoc.y, mTemplateMat.cols(), mTemplateMat.rows());

Imgproc.rectangle(mCameraMat, new Point(roi.x, roi.y), new Point(roi.width - 2, roi.height - 2), new Scalar(255, 0, 0, 255), 2);

return mCameraMat;

上一篇下一篇

猜你喜欢

热点阅读