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

--

Search Discussions

  • Andrey Pavlenko at Dec 17, 2012 at 11:45 am
    The problem is that the `matchTemplate<http://docs.opencv.org/trunk/modules/imgproc/doc/object_detection.html#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<http://docs.opencv.org/trunk/modules/core/doc/operations_on_arrays.html#normalize>(mResult,
    mResult8U, 0, 255, NORM_MINMAX, CV_8U);`*
    Then you can convert *mResult8U* to Bitmap.

    --
  • subash Yaish at Dec 18, 2012 at 10:29 am
    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);
    }

    On Monday, December 17, 2012 1:45:55 PM UTC+2, Andrey Pavlenko wrote:

    The problem is that the `matchTemplate<http://docs.opencv.org/trunk/modules/imgproc/doc/object_detection.html#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<http://docs.opencv.org/trunk/modules/core/doc/operations_on_arrays.html#normalize>(mResult,
    mResult8U, 0, 255, NORM_MINMAX, CV_8U);`*
    Then you can convert *mResult8U* to Bitmap.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupandroid-opencv @
categoriesandroid
postedDec 17, '12 at 8:32a
activeDec 18, '12 at 10:29a
posts3
users2
websiteandroid.com

2 users in discussion

subash Yaish: 2 posts Andrey Pavlenko: 1 post

People

Translate

site design / logo © 2017 Grokbase