Ask Your Question
0

Drawing rectangles from contours in Android

asked 2019-01-18 04:38:41 -0600

MCozhusheck gravatar image

I am trying to detect moving objects by drawing rectangle around them using openCV in Android App with BackgroundSubtractorMOG2 object.

I have already extracted foreground mask and found contours but It seems like that Imgproc.rectangle() doesn't draw rectangles

override fun onCameraFrame(inputFrame: CvCameraViewFrame?): Mat? {
        val minContourWidth = 35
        val minContourHeight = 35
        val threshold = 100.0
        val kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, Size(5.0, 5.0))

        frame = inputFrame?.rgba()
        backSub?.apply(frame, fgMask)
        Imgproc.morphologyEx(fgMask, fgMask, Imgproc.MORPH_CLOSE, kernel) // fill holes
        Imgproc.morphologyEx(fgMask, fgMask, Imgproc.MORPH_OPEN, kernel) //remove noise
        Imgproc.dilate(fgMask, fgMask, kernel)

        val cannyOutput = Mat()
        Imgproc.Canny(fgMask, cannyOutput, threshold, threshold * 2)
        val contours = ArrayList<MatOfPoint>()
        val hierarchy = Mat()
        Imgproc.findContours(cannyOutput, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1)
        hierarchy.release()
        Log.i(TAG, "contours size: " + contours.size)

        for(contour in contours) {

            val approxCurve = MatOfPoint2f()
            val contour2f = MatOfPoint2f()
            contour.convertTo(contour2f, CvType.CV_32FC2)
            val approxDistance = Imgproc.arcLength(contour2f, true) * 0.02
            Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true)
            val points = MatOfPoint()
            approxCurve.convertTo(points, CvType.CV_8UC4)
            val rect = Imgproc.boundingRect(points)

            Imgproc.rectangle(frame, Point(rect.x.toDouble(), rect.y.toDouble()),
                Point((rect.x + rect.width).toDouble(), (rect.y + rect.height).toDouble()), Scalar(255.0, 0.0, 0.0, 255.0), 3)
        }

        return frame
    }

Link to my repository

edit retag flag offensive close merge delete

Comments

nothing wrong with your rectangle() call. you'll have to debug the inputs.

are there any contours ? what happens in your poly approximation ? what is the bbox ?

berak gravatar imageberak ( 2019-01-18 04:55:02 -0600 )edit

what's the reason for:

approxCurve.convertTo(points, CvType.CV_8UC4)

? that looks plain wrong !

berak gravatar imageberak ( 2019-01-18 06:17:07 -0600 )edit
1

I get IllegalArgumentException: Incompatible Mat while calling val contour2f = MatOfPoint2f(contour) so found this solution. It made my code compile, but I'm not really sure what it does.

MCozhusheck gravatar imageMCozhusheck ( 2019-01-18 06:48:21 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-01-19 03:03:10 -0600

berak gravatar image

updated 2019-01-19 03:04:20 -0600

you had a contour.convertTo(contour2f, CvType.CV_32FC2) to go from integer to float points, so you need a approxCurve.convertTo(points, CvType.CV_32SC2) to convert it back

edit flag offensive delete link more

Comments

Thanks, that works !

MCozhusheck gravatar imageMCozhusheck ( 2019-01-19 03:55:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-18 04:38:41 -0600

Seen: 1,984 times

Last updated: Jan 19 '19