簡體   English   中英

模板匹配-為什么結果矩陣小於指定的矩陣?

[英]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, ...]

我的問題是:

  1. 為什么mResult尺寸小於mImage盡管已經宣布mResult大小是根據mImage大小?
  2. 事實證明,通過使用CV_8UC1類型,內容只能以黑色或白色顯示,而mResult應該具有浮點值,但Utils.matToBitmap方法不支持CV_8UC1CV_8UC3CV_8UC4以外的mat類型。 有沒有什么辦法來顯示CV_32FC1為位圖,它顯示的實際灰度mResult

盡管已經聲明mResult的大小基於mImage的大小,但為什么mResult的大小小於mImage?

由於模板匹配基本上是空間卷積,因此當對高度為Hh圖像執行h ,結果將為H-h+1 與結果寬度( W-w+1 )相同。 但是您仍然可以模板匹配后將結果resize(mImage.rows(), mImage.cols())

事實證明,通過使用CV_8UC1類型,內容只能以黑色或白色顯示,而mResult應該具有浮點值,但Utils.matToBitmap方法不支持CV_8UC1,CV_8UC3和CV_8UC4以外的其他Mat類型。 有什么方法可以顯示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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM