因此,我正在使用OpenCVCameraView
为输入图像中的特定区域进行模板匹配。以下是我的代码的样子。模板匹配 - 为什么结果矩阵小于指定值?
Mat input;
Rect bigRect = ...; //specific size
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
input = inputFrame.rgba();
...
}
public void Template(View view) {
Mat mImage = input.submat(bigRect);
Mat mTemplate = Utils.loadResource(this, R.id.sample, Highgui.CV_LOAD_IMAGE_COLOR);
Mat mResult = new Mat(mImage.rows(), mImage.cols(), CvType.CV_32FC1); // I use the same size as mImage because mImage's size is already smaller than inputFrame
Imgproc.cvtColor(mImage, mImage, Imgproc.COLOR_RGBA2RGB); //convert is needed to make mImage and mTemplate to be the same type
Imgproc.matchTemplate(mImage, mTemplate, mResult, match_method);
Core.normalize(mResult, mResult, 0, 1, Core.NORM_MINMAX, -1, new Mat());
mResult.convertTo(mResult, CvType.CV_8UC1); // I convert the matrix because I need to show it to imageview via bitmap
Bitmap bmResult1 = Bitmap.createBitmap(mImage.width(), mImage.height(), Bitmap.Config.RGB_565);
Bitmap bmResult2 = Bitmap.createBitmap(mResult.width(), mResult.height(), Bitmap.Config.RGB_565);
Utils.matToBitmap(mImage, bmResult1);
Utils.matToBitmap(mResult, bmResult2);
ImageView1.setImageBitmap(bmResult1);
ImageView2.setImageBitmap(bmResult2);
}
的我试图输出使用toString()
矩阵,并得到这些结果:
mImage: Mat [250*178*CV_8UC3, isCont=true, isSubmat=false, ...]
mResult: Mat [180*94*CV_8UC1, isCont=true, usSubmat=false, ...]
而且我的问题是:
- 为什么
mResult
大小比mImage
较小,尽管已经声明该mResult
大小是基于mImage
大小? - 事实证明,通过使用
CV_8UC1
类型,内容只有黑色和白色可供选择,而mResult应该有浮点值,但Utils.matToBitmap
方法不支持大于CV_8UC1
,CV_8UC3
,并CV_8UC4
其他垫类型。有没有什么办法显示CV_32FC1
位图,它显示mResult
的真实灰度?
opencv文档说:'结果 - 比较结果的地图。它必须是单通道32位浮点。如果图像是W \ times H并且templ是w \ times h,那么结果是(W-w + 1)\ times(H-h + 1).'所以我猜你的模板的大小是[69,83]? ? – Micka