Ask Your Question
0

Conversion of templateMatching result to Android Bitmap

asked 2012-12-16 04:08:08 -0600

subash gravatar image

updated 2012-12-17 06:41:07 -0600

Andrey Pavlenko gravatar image

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. here 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 because 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? is it something else?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2012-12-17 06:36:35 -0600

Andrey Pavlenko gravatar image

The problem is that the matchTemplate() result is a float point single channel Mat (CV_32FC1) that cannot be directly converted to Android Bitmap.

To convert such a Mat to Bitmap you need to call normalize(mResult, mResult8U, 0, 255, NORM_MINMAX, CV_8U);

Then you can convert mResult8U to Bitmap.

edit flag offensive delete link more

Comments

thanks, as you said 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);

}

subash gravatar imagesubash ( 2012-12-17 06:54:48 -0600 )edit
1

answered 2012-12-18 06:35:00 -0600

subash gravatar image

thanks, as you said 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);

}

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-16 04:08:08 -0600

Seen: 1,189 times

Last updated: Dec 18 '12