1

i'm developing to Android using openCV in Eclipse. I'm trying do template matching Frame a Frame. I can't convert the template and doing the match template on it. I'm using that function:

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

 private String getFileAbsPath(String fileName) {
   File f = new File(cacheDir, fileName);
   return f.getAbsolutePath();
}

I get a error on:

Imgproc.cvtColor(templ, (Mat) template, Imgproc.COLOR_BGR2RGBA);

My image is that (it's number 1): https://drive.google.com/file/d/0B5tH_Qo3-GvhaV9QSTUteXFiQmM/view?usp=sharing

Next, I've my method:

@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() - ((Mat) template).cols() + 1;
   int result_rows = src.rows() - ((Mat) template).rows() + 1;
   Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

  // Do the Matching and Normalize
  Imgproc.matchTemplate(src, (Mat) template, result, match_method);
  Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

  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, ((Mat) template).cols(), ((Mat) 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;

}

I get a error on that line:

Imgproc.matchTemplate(src, (Mat) template, result, match_method);

I can't do the match, don't know why ... Cans someone help me ?

NatsuDragonEye
  • 109
  • 5
  • 18
  • The conversion from BGR to RGBA requires the destination `Mat` to have 4 channels. Are you you want to do this? – karlphillip Oct 10 '14 at 01:21
  • I really don't know, but where i read yes, he said that I need that conversion. I read here: http://stackoverflow.com/questions/18336673/opencv-android-template-matching-from-camera – NatsuDragonEye Oct 10 '14 at 09:39

0 Answers0