来自摄像头的OpenCV Android模板匹配[英] OpenCV Android template matching from camera

本文是小编为大家收集整理的关于来自摄像头的OpenCV Android模板匹配的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试将图像与Android中的相机输入匹配.当我尝试用2个图像尝试一切,一切都很好.但现在我喜欢用相机输入做同样的事情.要完成此操作,我实现了CVCameraviewListener2并尝试了以下代码:

@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    int match_method = Imgproc.TM_CCOEFF;

    mSizeRgba = mRgba.size();

    int rows = (int) mSizeRgba.height;
    int cols = (int) mSizeRgba.width;

    Mat templ = Highgui.imread(getFileAbsPath("template.jpg"));

    // Create the result matrix
    int result_cols = cols - templ.cols() + 1;
    int result_rows = rows - templ.rows() + 1;

    Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

    Mat src = new Mat(result_rows, result_cols, CvType.CV_32F);
    mRgba.convertTo(src, CvType.CV_32F);

    Mat template = new Mat(templ.rows(), templ.cols(), CvType.CV_32F);
    templ.convertTo(template, CvType.CV_32F);

    // Do the Matching and Normalize
    Imgproc.matchTemplate(src, templ, 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;
    }
    Rect roi = new Rect((int) matchLoc.x, (int) matchLoc.y, templ.cols(),
            templ.rows());
    // Mat cropped = new Mat(mRgba, roi);
    Core.rectangle(result, new Point(roi.x, roi.y), new Point(roi.width - 2, roi.height - 2), new Scalar(255, 0, 0, 255), 2);
    return mRgba;
}

当我运行此代码时,我得到这个OpenCV错误:

OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) 
                                          && img.type() == templ.type()) in ...

任何人都可以帮助我解决这个问题吗?

感谢

推荐答案

我解决了我的问题.我需要将模板颜色从BGR转换为RGBA. 使用以下代码,我不再崩溃,但预览中的相机帧非常慢.这不是我想要的.

    public void initialize(){
    if (src.empty())
        return;
    if(template == null){
        Mat templ = Highgui.imread(getFileAbsPath("template.png"), Highgui.CV_LOAD_IMAGE_UNCHANGED);
        template = new Mat(templ.size(), CvType.CV_32F);
        Imgproc.cvtColor(templ, template, Imgproc.COLOR_BGR2RGBA);
    }
}

@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    src = inputFrame.rgba();
    initialize();
    int match_method = Imgproc.TM_SQDIFF;

    // Create the result matrix
    int result_cols = src.cols() - template.cols() + 1;
    int result_rows = src.rows() - template.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

    // Do the Matching and Normalize
    Imgproc.matchTemplate(src, template, 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;
    }

    Rect roi = new Rect((int) matchLoc.x, (int) matchLoc.y, template.cols(), template.rows());
    Core.rectangle(src, new Point(roi.x, roi.y), new Point(roi.width - 2, roi.height - 2), new Scalar(255, 0, 0, 255), 2);
    return src;
}

其他推荐答案

该错误表明模板,SRC在通道和大小方面不兼容.

// Do the Matching and Normalize
    Imgproc.matchTemplate(src, templ, result, match_method);

你确定它应该是temp1吗?似乎您对template

进行了大量转换

本文地址:https://www.itbaoku.cn/post/102560.html

问题描述

I'm trying to match an image with the camera input in Android. When I try this with 2 images everything works just fine. But now I like to do the same thing with the camera input. To get this done I implemented CvCameraViewListener2 and tried the following code:

@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    int match_method = Imgproc.TM_CCOEFF;

    mSizeRgba = mRgba.size();

    int rows = (int) mSizeRgba.height;
    int cols = (int) mSizeRgba.width;

    Mat templ = Highgui.imread(getFileAbsPath("template.jpg"));

    // Create the result matrix
    int result_cols = cols - templ.cols() + 1;
    int result_rows = rows - templ.rows() + 1;

    Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

    Mat src = new Mat(result_rows, result_cols, CvType.CV_32F);
    mRgba.convertTo(src, CvType.CV_32F);

    Mat template = new Mat(templ.rows(), templ.cols(), CvType.CV_32F);
    templ.convertTo(template, CvType.CV_32F);

    // Do the Matching and Normalize
    Imgproc.matchTemplate(src, templ, 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;
    }
    Rect roi = new Rect((int) matchLoc.x, (int) matchLoc.y, templ.cols(),
            templ.rows());
    // Mat cropped = new Mat(mRgba, roi);
    Core.rectangle(result, new Point(roi.x, roi.y), new Point(roi.width - 2, roi.height - 2), new Scalar(255, 0, 0, 255), 2);
    return mRgba;
}

When I run this code I get this OpenCV error:

OpenCV Error: Assertion failed ((img.depth() == CV_8U || img.depth() == CV_32F) 
                                          && img.type() == templ.type()) in ...

Can anyone help me with this problem?

Thanks

推荐答案

I solved my problem. I needed to convert the color of the template from BGR to RGBA. With the following code i have no crashes anymore but the camera frames in the preview are very slow. This is not exactly what I want.

    public void initialize(){
    if (src.empty())
        return;
    if(template == null){
        Mat templ = Highgui.imread(getFileAbsPath("template.png"), Highgui.CV_LOAD_IMAGE_UNCHANGED);
        template = new Mat(templ.size(), CvType.CV_32F);
        Imgproc.cvtColor(templ, template, Imgproc.COLOR_BGR2RGBA);
    }
}

@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    src = inputFrame.rgba();
    initialize();
    int match_method = Imgproc.TM_SQDIFF;

    // Create the result matrix
    int result_cols = src.cols() - template.cols() + 1;
    int result_rows = src.rows() - template.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

    // Do the Matching and Normalize
    Imgproc.matchTemplate(src, template, 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;
    }

    Rect roi = new Rect((int) matchLoc.x, (int) matchLoc.y, template.cols(), template.rows());
    Core.rectangle(src, new Point(roi.x, roi.y), new Point(roi.width - 2, roi.height - 2), new Scalar(255, 0, 0, 255), 2);
    return src;
}

其他推荐答案

The error suggests the template and the src are not compatible in terms of channels and size.

// Do the Matching and Normalize
    Imgproc.matchTemplate(src, templ, result, match_method);

Are you sure it should be temp1? It seems you performed a lot of conversion to template