/**
* Checks if the pattern can be found in the given area of interest and sets up feedback.
* @param match_method Match Methods supported by OpenCV.
* @param res ID of the resource pattern in the res directory.
* @param resName Name of the resource to be returned.
* @param thresh Threshold the best detection has to pass in order to be a successful detection.
*/
public void templateMatching(int match_method, int res, String resName, double thresh) {
// Pattern Matching
Point matchLocCode; double matchValCode;
Log.i("HERE", "" + resName);
Rect roiCodeArea = new Rect(new Point(showBit.cols() * card.getPattern(resName).getTl().x,showBit.rows() * card.getPattern(resName).getTl().y), new Point(showBit.cols() * card.getPattern(resName).getBr().x, showBit.rows() * card.getPattern(resName).getBr().y));
Mat cropedCodeArea = showBit.submat(roiCodeArea);
Bitmap bmCode = BitmapFactory.decodeResource(getResources(), res);
Mat cropedCode = new Mat ( bmCode.getHeight(), bmCode.getWidth(), CvType.CV_8U, new Scalar(4));
Utils.bitmapToMat(bmCode, cropedCode);
int result_cols_code = cropedCodeArea.cols() - cropedCode.cols() + 1;
int result_rows_code = cropedCodeArea.rows() - cropedCode.rows() + 1;
Mat resultCode = new Mat(result_rows_code, result_cols_code, CvType.CV_32FC1);
Imgproc.matchTemplate(cropedCodeArea, cropedCode, resultCode, match_method);
MinMaxLocResult mmrCode = Core.minMaxLoc(resultCode);
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLocCode = mmrCode.minLoc;
matchValCode = mmrCode.minVal;
} else {
matchLocCode = mmrCode.maxLoc;
matchValCode = mmrCode.maxVal;
}
Log.w("matchValCode", "" + matchValCode);
// Pattern passes Detection
if(matchValCode >= thresh) {
// If detecting card and pattern passes: get the card that pattern belongs to.
if(cardType.equals("-DETECT-")) {
cardType = getFoundCard(resName);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(
CardValidationActivity.this,
"Card detected: " + cardType,
Toast.LENGTH_LONG
).show();
}
});
}
else {
Core.rectangle( cropedCodeArea, matchLocCode, new Point( matchLocCode.x + cropedCode.cols() , matchLocCode.y + cropedCode.rows() ), new Scalar(0, 255, 0, 255), 4 );
theText.put("Pattern: " + resName, "PASSED");
}
}
else {
theText.put("Pattern: " + resName, "FAILED");
failureCount++;
}
}
CardValidationActivity.java 文件源码
java
阅读 28
收藏 0
点赞 0
评论 0
项目:Pixtern-Library
作者:
评论列表
文章目录