4

I am trying to create a template matching function on Android using OpenCV with Java (not with native).

My problem is displaying the image. The class mattoBitmap works (in Java) but if I want to convert the result of the template matching function I get a FATAL EXCEPTION when I call the Utils.matToBitmap function.

Below is the relevant code:

void TemplateMatch() {

    mFind = new Mat(256, 192, CvType.CV_8UC4);
    Input = new Mat(256, 192, CvType.CV_8UC4);

    mResult = new Mat(217, 153, CvType.CV_8UC4); // (bmp2 size is 40)

    Utils.bitmapToMat(bmp2, mFind);
    Utils.bitmapToMat(bmp1, Input);

    Imgproc.matchTemplate(mFind, Input, mResult, Imgproc.TM_SQDIFF);

    bmp3 = Bitmap.createBitmap(mResult.cols(), mResult.rows(), conf);

    Utils.matToBitmap(mResult, bmp3);

    iv2.setImageBitmap(bmp3);

}

The size of mResult to my knowledge is not important when it is created becuase it is set afterwards by the template match function.

Do I need to convert the mResult mat to something before I convert it to bmp?

Do I need to convert the bmp to something before I can convert the mat to it?

Larra Artea
  • 103
  • 1
  • 3
  • 9
user1768360
  • 193
  • 1
  • 2
  • 12

2 Answers2

2

You need to convert bitmap to RGBA format and vice versa. Maybe you need to take a look here: https://groups.google.com/group/android-opencv/ and here: Java openCV - Error while conevrting Bitmap to Mat

Community
  • 1
  • 1
Nick Robertson
  • 1,047
  • 4
  • 18
  • 41
  • i did find out that there is a convertion problem but i dont know what to convert – user1768360 Dec 16 '12 at 12:34
  • 1
    thanks, the second link does not help me, i have included the libraries and i can convert bitmap to mat. i can also convert mat to bitmap just not the specific type received from "matchTemplate". (type recieved is 5, when i convert it to 24 like the other bitmaps i get a black photo). – user1768360 Dec 16 '12 at 12:46
2

The problem is that the matchTemplate() result is a float point single channel Mat so I needed to normalize the mResult vector. the solution is:

void TemplateMatch(){

mFind=new Mat(256, 192, CvType.CV_8UC4); 
Input = new Mat(256, 192, CvType.CV_8UC4); 

Mat mResult8u = new Mat(256, 192, CvType.CV_8UC4); 

mResult = new Mat(217, 153, CvType.CV_8UC4); 

Utils.bitmapToMat(bmp2, mFind);
Utils.bitmapToMat(bmp1, Input);


Imgproc.matchTemplate(mFind, Input, mResult, Imgproc.TM_SQDIFF) ;
bmp3= Bitmap.createBitmap(mResult.cols(),  mResult.rows(),Bitmap.Config.ARGB_8888);
Core.normalize(mResult, mResult8u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);
Utils.matToBitmap(mResult8u, bmp3);
iv2.setImageBitmap(bmp3);

}
user1768360
  • 193
  • 1
  • 2
  • 12