如何通过使用旋转模板图像来匹配图像中的图像点并返回最接近的值。 我想匹配模板图像和输入图像。 通过使用旋转模板图像来匹配匹配点。 模板图像每度旋转一次。 匹配的返回值是float并保存在数组列表中。 如何使用Open CV android?
private Bitmap MatchImage(int match_method){
Bitmap bitmapInput = BitmapFactory.decodeFile(path+"/input_image_pj.bmp");
Mat matInput = new Mat ( bitmapInput.getHeight(), bitmapInput.getWidth(), CvType.CV_8U);
Bitmap bitmapCopyInput = bitmapInput.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bitmapCopyInput, matInput);
Bitmap tempImage = BitmapFactory.decodeResource(this.getResources(),R.drawable.image_template);
Mat matTemp = new Mat ( tempImage.getHeight(), tempImage.getWidth(), CvType.CV_8U);
Bitmap bitmapCopyTemp = tempImage.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bitmapCopyTemp, matTemp);
rotateTemp(matTemp,1);
int result_cols = matInput.cols() - matTemp.cols() + 1;
int result_rows = matInput.rows() - matTemp.rows() + 1;
Mat matResult = new Mat(result_rows, result_cols, CvType.CV_32FC1);
Imgproc.matchTemplate(matInput, matTemp, matResult, match_method);
Core.normalize(matResult, matResult, 0, 1, Core.NORM_MINMAX, -1, new Mat());
Toast.makeText(this, "Result : "+matResult, Toast.LENGTH_LONG).show();
MinMaxLocResult mmr = Core.minMaxLoc(matResult);
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
Core.rectangle(matInput, matchLoc, new Point(matchLoc.x + matTemp.cols(),
matchLoc.y + matTemp.rows()), new Scalar(0, 255, 0));
// Save the visualized detection.
System.out.println("Writing "+ resulfFile);
Highgui.imwrite(path+resulfFile, matInput);
Bitmap resultBitmap = Bitmap.createBitmap(matInput.cols(), matInput.rows(),Bitmap.Config.ARGB_8888);;
Utils.matToBitmap(matInput, resultBitmap);
bitmapInput = resultBitmap;
return resultBitmap;
}
private Mat rotateTemp(Mat matRotate,int degMore){
//Rotate Image
int degrees = 30 + degMore;
Point center = new Point(matRotate.cols()/2, matRotate.rows()/2);
Mat rotImage = Imgproc.getRotationMatrix2D(center, degrees, 1.0);
Imgproc.warpAffine(matRotate, matRotate, rotImage, matRotate.size());
//mat is rotated
return matRotate;
}