我在这里看到了一些与我的错误相关的问题,例如this和this,而且我知道我无法执行Imgproc.matchTemplate()
方法图像和模板不具有相同的数据类型。但我仍然对如何知道我使用的Mat
类型感到困惑。
以下是我根据示例here改编的代码:
for (int i = 0; i < 24; i++) {
arrDraw[i] = getResources().getIdentifier("let" + i, "drawable", getPackageName());
}
Mat mImage = input.submat(bigRect);
for (int i = 0; i < 24; i++) {
Mat mTemplate = Utils.loadResource(this, arrDraw[i], Highgui.CV_LOAD_IMAGE_COLOR);
Mat mResult = new Mat(mImage.rows(), mImage.cols(), CvType.CV_32FC1);
Imgproc.matchTemplate(mImage, mTemplate, mResult, match_method);
Core.normalize(mResult, mResult, 0, 1, Core.NORM_MINMAX, -1, new Mat());
... // further process
}
所以基本上我尝试做的是从mImage
的submat中取inputFrame
并与其他24张图片匹配模板进程并确定哪个具有最佳价值(最低或者最高)。但错误表明了这一点。
OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type()) in void cv::matchTemplate(cv::InputArray, cv::InputArray, cv::OutputArray, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/templmatch.cpp, line 249
我尝试使用相同的类型初始化mImage
和mTemplate
,但仍然没有运气。有什么建议?谢谢。
答案 0 :(得分:2)
错误告诉您图像和模板有不同的类型。
Assertion failed ... img.type() == templ.type() ....
我愿意下注(少量)mTemplate
订购CV_8UC3 BGR。
从您发布的代码中,如果从相机框架中提取,则无法告诉mImage
的类型是什么 如果你做了类似的事情:
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat inputFrame = inputFrame.rgba();
....
}
然后它可能是CV_8UC4 BGRA订购。这是不一样的类型。
另外,我不确定submat()的行为是3D还是4D输入矩阵,我认为它的设计只能在2D矩阵上运行,所以你可能会发现它返回一个2D矩阵(CV_8UC2)或一些未定义的古怪。
我建议您在type()
来电之前尝试转储depth()
和matchTemplate( ... )
或同时转发图像和模板。