我想匹配Android中的相機輸入圖像。當我用2張圖片嘗試這一切時,一切正常。但現在我喜歡用相機輸入做同樣的事情。要完成這件事,我實現CvCameraViewListener2和嘗試下面的代碼:OpenCV Android模板匹配從相機
@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 ...
誰能幫我解決這個問題?
謝謝
事實上,這是一種嘗試,以獲得墊對象兼容,但可能是錯誤的方式... Imgproc.matchTemplate(SRC,模板,結果,match_method);給出相同的錯誤。 – Smek
我的意思是,我建議'Imgproc.matchTemplate(src,template,result,match_method);' – SolessChong
是的,我嘗試過,但它不起作用。我測試了它們不匹配的Mat對象src和模板的類型。我怎樣才能將它們轉換爲相同的類型? – Smek