Ask Your Question
0

Trouble converting mat to bitmap?

asked 2017-09-10 13:09:55 -0600

I am using OpenCV to detect the similarities between two images. Unfortunately, my line of code that converts my Mat back to a bitmap is causing my app to crash. Does anyone have any ideas what would be causing this? Here is my code:

    Bitmap centerBit = BitmapFactory.decodeResource(getResources(), R.drawable.center);
    Mat center = new Mat();
    Utils.bitmapToMat(centerBit,center);
    Mat resultCenter = new Mat();
    Imgproc.matchTemplate(center,center,resultCenter,Imgproc.TM_SQDIFF_NORMED);
    Bitmap bm = Bitmap.createBitmap(resultCenter.cols(), resultCenter.rows(),Bitmap.Config.ARGB_    8888);
    Utils.matToBitmap(resultCenter, bm); // THIS LINE CREATING ERROR
    ImageView iv = (ImageView) findViewById(R.id.imageView);
    iv.setImageBitmap(bm);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-12 01:39:21 -0600

berak gravatar image

the result Mat from matchTemplate() is a float Mat, containing the probability distribution for finding a match for each point.

this is not really an "image", you''re not supposed to convert it into a bitmap, but you should use minMaxLoc(), to determine the most likely position of your match

IF you still want to visualize it somehow, you'll have to convert it to CV_8U (using a scale factor like 255), then also you'll have to adapt the flag in Bitmap.createBitmap, because it has only a single channel (it is not BGRA or such)

please have a look at the tutorial

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-10 13:09:55 -0600

Seen: 1,229 times

Last updated: Sep 12 '17