2013-08-20 127 views
1

我想匹配Android中的相机输入图像。当我用2张图片尝试这一切时,一切正常。但现在我喜欢用相机输入做同样的事情。要完成这件事,我实现CvCameraViewListener2和尝试下面的代码:OpenCV Android模板匹配从相机

@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 ... 

谁能帮我解决这个问题?

谢谢

回答

1

错误表明模板和src在通道和大小方面不兼容。

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

您确定应该是temp1?看来你执行了很多转换为template

+0

事实上,这是一种尝试,以获得垫对象兼容,但可能是错误的方式... Imgproc.matchTemplate(SRC,模板,结果,match_method);给出相同的错误。 – Smek

+0

我的意思是,我建议'Imgproc.matchTemplate(src,template,result,match_method);' – SolessChong

+0

是的,我尝试过,但它不起作用。我测试了它们不匹配的Mat对象src和模板的类型。我怎样才能将它们转换为相同的类型? – Smek

2

我解决了我的问题。我需要将模板的颜色从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; 
} 
+0

什么是使其更好的替代方法? –