模板匹配 - 为什么结果矩阵小于指定值?
Template Matching - Why the result matrix is smaller than specified?
所以我正在使用 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
显示给Bitmap,它显示mResult
的真实灰度?
Why the mResult size is smaller than mImage despite already declared
that mResult size is based on mImage size?
由于模板匹配基本上是一个空间卷积,当对高度为 H
和 h
的图像执行时,结果将是 H-h+1
。与结果宽度相同 (W-w+1
)。但是你仍然可以resize
结果返回(mImage.rows(), mImage.cols())
在模板匹配之后。
Turns out that by using CV_8UC1 type, the content is only available in
black or white, while mResult is supposed to have floating value, but
Utils.matToBitmap method doesn't support mat types other than CV_8UC1,
CV_8UC3, and CV_8UC4. Is there any way to show CV_32FC1 to Bitmap that
it shows the real grayscale of mResult?
关键就在这两行,我觉得:
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
您不能将其规范化为取 0 到 255 之间的值吗?
Core.normalize(mResult, mResult, 0, 255, Core.NORM_MINMAX, -1, new Mat());
所以我正在使用 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
显示给Bitmap,它显示mResult
的真实灰度?
Why the mResult size is smaller than mImage despite already declared that mResult size is based on mImage size?
由于模板匹配基本上是一个空间卷积,当对高度为 H
和 h
的图像执行时,结果将是 H-h+1
。与结果宽度相同 (W-w+1
)。但是你仍然可以resize
结果返回(mImage.rows(), mImage.cols())
在模板匹配之后。
Turns out that by using CV_8UC1 type, the content is only available in black or white, while mResult is supposed to have floating value, but Utils.matToBitmap method doesn't support mat types other than CV_8UC1, CV_8UC3, and CV_8UC4. Is there any way to show CV_32FC1 to Bitmap that it shows the real grayscale of mResult?
关键就在这两行,我觉得:
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
您不能将其规范化为取 0 到 255 之间的值吗?
Core.normalize(mResult, mResult, 0, 255, Core.NORM_MINMAX, -1, new Mat());