Kotlin: error on converting mat to bitmap

Hey! I’ve been trying to convert Mat to bitmap but it’s not working.

I’m using the OpenCV’s Utils class to convert it to mat.

 fun detectPerson(img: Bitmap, folderName: String) {
        val frame = Mat()
        val originalMat = Mat()

        Utils.bitmapToMat(img, originalMat)
        Utils.bitmapToMat(img, frame)

        Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGBA2RGB)

        val blob = Dnn.blobFromImage(
            Size(416.0, 416.0),
            Scalar(0.0, 0.0, 0.0),/*swapRB*/
            true, /*crop*/



 private fun convertMatToBitMap(input: Mat): Bitmap? {
        var bmp: Bitmap? = null
        val rgb = Mat()
//        Imgproc.cvtColor(input, rgb, Imgproc.COLOR_BGR2RGB)
        try {
            bmp = Bitmap.createBitmap(rgb.cols(), rgb.rows(), Bitmap.Config.ARGB_8888)
            Utils.matToBitmap(rgb, bmp)
        } catch (e: CvException) {
        return bmp
error received is this
 java.lang.IllegalArgumentException: width and height must be > 0
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1060)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:1027)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:977)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:938)
        at com.buttonwillow.app.views.main.camera.SeatBeltDemoActivity.convertMatToBitMap

rgb is empty / invalid, if you comment the cvtColor() away
(so, rgb.cols() == 0, no bitmap created)

next, the blob is a 4d tensor (a batch of seperated color planes), not an image, you can’t use that as an input for your Bitmap conversion

then, is that really all of the person detection ?
you’d still have to pipe the blob through the network, and parse rectangles from the output tensor, right ?
(then maybe you could draw some rects into the original image, and show that instead)

Yes. I need to draw line on the bitmap. How do I do that? I have bouding box values but I need to draw that on the image. If possible could you please show me how to do that?

  val imgRoi = Mat(
                            Rect(x, y, x1 - x, y1 - y)

I have to show lines how do I do that? I need to draw Rect(x, y, x1 - x, y1 - y) on my bitmap

val originalMat = Mat()
        Utils.bitmapToMat(img, originalMat)

don’t crop it, instead draw a rectangle into originalMat frame. convert to bitmap.

1 Like

I’m using openCV346. and I can not see the specific function that you have mentioned. Can you also let me know give an example to show the bounding? thanks.

Like how do I convert my rect to point1 and point2? any suggestions?

i updated the link, have a look

public static void rectangle(Mat img,
                             Point pt1,
                             Point pt2,
                             Scalar color)

How do I convert a Rect object to point p1, point pt2. so that my bouding box get displayed?

no, i won’t answer this …

Anyhow thanks for your help. You’ve helped me enough already. :blush: