简体   繁体   English

Android-模板匹配

[英]Android - Template Matching

I want to create an android application. 我想创建一个Android应用程序。 Program steps are below 程序步骤如下

  1. Open camera 开放式摄像头
  2. Get frames 获取框架
  3. Select a frame by touch screen 通过触摸屏选择帧
  4. Load template image under drawable folder 将模板图像加载到可绘制文件夹下
  5. Apply template matching 应用模板匹配
  6. Show result 显示结果

The mat object of template image is not empty. 模板图像的mat对象不为空。 I check it. 我检查一下。 When I run this code, I get below error message. 当我运行此代码时,出现以下错误信息。

在此处输入图片说明

Code : 代码:

public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    temp = new Mat(height, width, CvType.CV_8UC4);
}

public boolean onTouch(View v, MotionEvent event) {
    int cols = mRgba.cols();
    int rows = mRgba.rows();

    int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2;
    int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2;

    int x = (int)event.getX() - xOffset;
    int y = (int)event.getY() - yOffset;

    Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")");

    if ((x < 0) || (y < 0) || (x > cols) || (y > rows)) return false;

    mIsColorSelected = true;
    return true; // don't need subsequent touch events
}

private static Mat readInputStreamIntoMat(InputStream inputStream) throws IOException {
    // Read into byte-array
    byte[] temporaryImageInMemory = readStream(inputStream);

    // Decode into mat. Use any IMREAD_ option that describes your image appropriately
    Mat outputImage = Highgui.imdecode(new MatOfByte(temporaryImageInMemory), Highgui.IMREAD_GRAYSCALE);

    return outputImage;
}

private static byte[] readStream(InputStream stream) throws IOException {
    // Copy content of the image to byte-array
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead;
    byte[] data = new byte[16384];

    while ((nRead = stream.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();
    byte[] temporaryImageInMemory = buffer.toByteArray();
    buffer.close();
    stream.close();
    return temporaryImageInMemory;
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    if(mIsColorSelected) {
   InputStream inpT = getResources().openRawResource(R.drawable.imgt);
   Mat mTemp;
        try {
            mRgba.copyTo(temp);
            mTemp = readInputStreamIntoMat(inpT);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // / Create the result matrix
        int result_cols = temp.cols() - mTemp.cols() + 1;
        int result_rows = temp.rows() - mTemp.rows() + 1;
        Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
        int match_method = 4;
        // / Do the Matching and Normalize
        Imgproc.matchTemplate(temp, mTemp, result, match_method);
          Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
     /*
         Localizing the best match with minMaxLoc
        MinMaxLocResult mmr = Core.minMaxLoc(result);

        Point matchLoc;
        if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
            matchLoc = mmr.minLoc;
        } else {
            matchLoc = mmr.maxLoc;
        }/*
        // / Show me what you got
        Core.rectangle(temp, matchLoc, new Point(matchLoc.x + mTemp.cols(),
                matchLoc.y + mTemp.rows()), new Scalar(0, 255, 0));*/
        return temp;
    }
    else {
        mRgba = inputFrame.rgba();
    }

    return mRgba;
}

For template matching , both source image and template image must be of same data type( 1 ). 对于模板匹配,源图像和模板图像必须具有相同的数据类型( 1 )。 Here your template image( mTemp ) is a gray scale image and source image( mRgba / temp ) is a color image with alpha channel. 这里,您的模板图像( mTemp )是灰度图像,源图像( mRgba / temp )是带有alpha通道的彩色图像。

So, lets change both source and template images to be gray scale images 因此,让我们将源图像和模板图像都更改为灰度图像

temp = new Mat(height, width, CvType.CV_8UC1);

and replace mRgba.copyTo(temp) with 并将mRgba.copyTo(temp)替换为

Imgproc.cvtColor(mRgba, temp, Imgproc.COLOR_RGBA2GRAY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM