Skip to content

Instantly share code, notes, and snippets.

@anthonykeane
Created July 11, 2013 03:27
Show Gist options
  • Save anthonykeane/5972302 to your computer and use it in GitHub Desktop.
Save anthonykeane/5972302 to your computer and use it in GitHub Desktop.
Pattern Matching OpenCV
// Pattern Matching (very Slow)
int match_method = Imgproc.TM_SQDIFF;
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.speedlimit55);
//battHeight and battWidth should have the height and width of your bitmap...
Mat mBatt = new Mat(mBitmap.getHeight(), mBitmap.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(mBitmap, mBatt);
// / Create the result matrix
int result_cols = mCameraFeed.cols() - mBatt.cols() + 1;
int result_rows = mCameraFeed.rows() - mBatt.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
// / Do the Matching and Normalize
Imgproc.matchTemplate(mCameraFeed, mBatt, 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;
}
// / Show me what you got
Core.rectangle(mCameraFeed, matchLoc, new Point(matchLoc.x + mBatt.cols(),
matchLoc.y + mBatt.rows()), new Scalar(0, 255, 0));
// // Save the visualized detection.
// System.out.println("Writing "+ outFile);
// Highgui.imwrite(outFile, img);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment