opencv +android template matching error

asked 2016-04-03 07:22:36 -0600

siline gravatar image

I am a beginner in opencv and android and i should do an application that find a template in a big image .I refered to some sites and i tried this project but i got a lot of errors:

image description

This is tha code

'public class MainActivity extends Activity implements OnClickListener{

String TAG = "Main";

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status)
{
    switch (status) {
        case LoaderCallbackInterface.SUCCESS:
        {
            Log.i(TAG, "OpenCV loaded successfully");
        } break;
        default:
        {
            super.onManagerConnected(status);
        } break;
    }
}
};

  @Override
   public void onResume()
    {
  super.onResume();
   OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mLoaderCallback);
 }

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

Log.i(TAG, "called onCreate");
        setContentView(R.layout.activity_main);

        //Buttons
        Button btnRunIt = (Button) findViewById(R.id.btnRunIt);
        btnRunIt.setOnClickListener(this);
}

@Override
public void onClick(View v) {
        // TODO Auto-generated method stub

        @SuppressWarnings("unused")
        Intent myIntent;
        int id = v.getId();

        switch (id)
        {
                case R.id.btnRunIt :

                        TemplateMatching matchIt = new TemplateMatching();

                        Uri inFile = Uri.parse("android.resource://com.example.match/drawable/img1.png");
                        Uri templateFile = Uri.parse("android.resource://com.example.match/drawable/template.png");
                        Uri outFile = Uri.parse("android.resource://com.example.match/drawable/img2.png");

                        String[] args = new String[3];
                        args[0] = inFile.toString();
                        args[1] = templateFile.toString();
                        args[2] = outFile.toString();

                        Bitmap image = matchIt.main(args);

                        ImageView imageView = (ImageView) findViewById(R.id.imageView1);

                        imageView.setImageBitmap(image);

                        break;
                default:
                        //Unrecognized button? We should never get here
        }
}

}'

public class MatchingDemo {

  public Mat run(String inFile, String templateFile, String outFile, int match_method) {

  System.out.println("\nRunning Template Matching");

  Mat img = Imgcodecs.imread(inFile);

   Mat templ =Imgcodecs.imread(templateFile);

   // / Create the result matrix

   int result_cols = img.cols() - templ.cols() + 1;

    int result_rows = img.rows() - templ.rows() + 1;

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

     // / Do the Matching and Normalize

     Imgproc.matchTemplate(img, 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;
   }
   // / Show me what you got
   Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
   matchLoc.y + templ.rows()), new Scalar(0, 255, 0));

   // Save the visualized detection.
  System.out.println("Writing "+ outFile);
  Imgcodecs.imwrite(outFile, img);



    return img;

}

         public class TemplateMatching {

    public Bitmap main(String[] args) {

        String infile = args[0].toString();
        String templatefile  = args[1].toString();
        String outfile = args[2].toString();

         Mat image = new MatchingDemo().run(infile, templatefile, outfile, Imgproc.TM_CCOEFF);

         Bitmap bitmap = Bitmap.createBitmap(image.cols(), image.rows(), Bitmap.Config.ARGB_8888);
         Utils.matToBitmap(image, bitmap);

         return bitmap;
    }
   }
edit retag flag offensive close merge delete