簡體   English   中英

來自相機的OpenCV Android模板匹配

[英]OpenCV Android template matching from camera

我正在嘗試將圖像與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

暫無
暫無

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

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