chamfer matching on android [closed]

asked 2015-07-15 00:35:39 -0600

thevinci gravatar image

updated 2018-10-04 23:48:33 -0600

Anyone here have tried implementing the chamerMatching method on android using opencv4android? I cant get it to work properly on my device galaxy tab 3 kitkat.

the application crashes as soon as I call the chamerMatching method.

here's the screenshot of the logcat from where the problem starts. image description

it seems like a memory problem.

I'm trying to create an application the will compute the similarity scores between two edge maps/matrices using the chamfer matching algorithm provided on the contrib folder of opencv 2.4.11.

heres my code.

public static float getScore(Mat img, Mat tpl)

    if (img.empty() | tpl.empty())
        System.out.println("Could not read image file.");
        return 0;

    Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2GRAY);
    Imgproc.cvtColor(tpl, tpl, Imgproc.COLOR_BGR2GRAY);

    Mat cimg = new Mat(img.rows(), img.cols(), CvType.CV_8UC1);
    Imgproc.cvtColor(img, cimg, Imgproc.COLOR_GRAY2BGR);

    List<MatOfPoint> results = new Vector<>();
    MatOfFloat costs = new MatOfFloat();

    int best = Contrib.chamerMatching(img, tpl, results, costs);

    if (best < 0) {
        System.out.println("matching not found");
        return 0;

    System.out.println("Chamfer Distance = " + costs.toList().get(best));

    for (int i =0; i < results.get(best).rows(); i++)
    {, results.get(best).toArray()[i], 0, new Scalar(0,255,0));

    int count_contour = Core.countNonZero(img);

    Imgproc.cvtColor(cimg, cimg, Imgproc.COLOR_BGR2GRAY);
    Imgproc.threshold(cimg, cimg, 150, 255, Imgproc.THRESH_BINARY);
    int count_white = Core.countNonZero(cimg);

    System.out.println("Contour = " + count_contour);
    System.out.println("White = " + count_white);

    float m = count_contour - count_white;
    float d = m / count_contour;
    float s = d * 100;

    return s;

heres the code that will call method getScore.

easyBtn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v){

                    Mat img = new Mat(257,257, CvType.CV_8UC1);
                    Mat tpl = new Mat(257,257, CvType.CV_8UC1);

                    Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo_in_clutter);
                    Bitmap tplBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);

                    Utils.bitmapToMat(imgBitmap, img);
                    Utils.bitmapToMat(tplBitmap, tpl);

                    Log.i("CHAMFER TEST", "Score = " + ChamferTest.getScore(img, tpl));

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-10-16 17:33:43.934219


it's probably unrelated to android. i have never met anyone here, who used that function successfully, and it was entirely removed in 3.0

berak gravatar imageberak ( 2015-07-15 01:12:24 -0600 )edit

is there any alternative that i can use? I'm planning to compare a hand drawn image(sketch) vs contour extracted from an image. I would like to get the similarity score.

thevinci gravatar imagethevinci ( 2015-07-15 01:27:49 -0600 )edit
berak gravatar imageberak ( 2015-07-15 03:26:21 -0600 )edit