[英]java adding two int arrays with different lengths. why giving a result that is smaller than correct result?
[英]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
以外的mat類型。 有沒有什么辦法來顯示CV_32FC1
為位圖,它顯示的實際灰度mResult
? 盡管已經聲明mResult的大小基於mImage的大小,但為什么mResult的大小小於mImage?
由於模板匹配基本上是空間卷積,因此當對高度為H
和h
圖像執行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.