我正在尝试将图像与 Android 中的相机输入相匹配。当我尝试使用 2 张图片时,一切正常。但现在我喜欢用相机输入做同样的事情。为了完成这项工作,我实现了 CvCameraViewListener2 并尝试了以下代码:
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
int match_method = Imgproc.TM_CCOEFF;
mSizeRgba = mRgba.size();
int rows = (int) mSizeRgba.height;
int cols = (int) mSizeRgba.width;
Mat templ = Highgui.imread(getFileAbsPath("template.jpg"));
// Create the result matrix
int result_cols = cols - templ.cols() + 1;
int result_rows = rows - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);
Mat src = new Mat(result_rows, result_cols, CvType.CV_32F);
mRgba.convertTo(src, CvType.CV_32F);
Mat template = new Mat(templ.rows(), templ.cols(), CvType.CV_32F);
templ.convertTo(template, CvType.CV_32F);
// Do the Matching and Normalize
Imgproc.matchTemplate(src, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
// Localizing the best match with minMaxLoc
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, templ.cols(),
templ.rows());
// Mat cropped = new Mat(mRgba, roi);
Core.rectangle(result, new Point(roi.x, roi.y), new Point(roi.width - 2, roi.height - 2), new Scalar(255, 0, 0, 255), 2);
return mRgba;
}
当我运行这段代码时,我得到了这个 OpenCV 错误:
OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F)
&& img.type() == templ.type()) in ...
谁能帮我解决这个问题?
谢谢