2015-05-18 36 views
1

因此,我正在使用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, ...] 

而且我的问题是:

  1. 为什么mResult大小比mImage较小,尽管已经声明该mResult大小是基于mImage大小?
  2. 事实证明,通过使用CV_8UC1类型,内容只有黑色和白色可供选择,而mResult应该有浮点值,但Utils.matToBitmap方法不支持大于CV_8UC1CV_8UC3,并CV_8UC4其他垫类型。有没有什么办法显示CV_32FC1位图,它显示mResult的真实灰度?
+0

opencv文档说:'结果 - 比较结果的地图。它必须是单通道32位浮点。如果图像是W \ times H并且templ是w \ times h,那么结果是(W-w + 1)\ times(H-h + 1).'所以我猜你的模板的大小是[69,83]? ? – Micka

回答

1

为什么mResult规模尽管已经宣布 是mResult大小是根据mImage尺寸比mImage小吗?

作为模板匹配基本上是一个空间卷积,具有高度Hh的图像执行时,结果将是H-h+1。与结果宽度相同(W-w+1)。但是您仍然可以将resize的结果返回到(mImage.rows(), mImage.cols())之后模板匹配。

事实证明,通过使用CV_8UC1类型,其内容只适用于 黑色或白色,而mResult应该有浮点值,但 Utils.matToBitmap方法不支持除CV_8UC1其他垫类型, CV_8UC3和CV_8UC4。有没有什么办法可以显示CV_32FC1到位图, 它显示了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()); 
+0

调整结果如果结果可能会对解释造成危险。除非你确切地知道你为什么想这么做。 – Micka

+1

表示也许?我不知道。正如你所说,这可能不被推荐,但如果OP希望它达到那个尺寸,我只是指出了如何去做。 –

+1

我看了OpenCV教程[这里](http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html#results),它看起来像结果马具有图像垫一样的尺寸所以我认为它代表性更好。 –