Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to get xpos and ypos value using FindContours

Hello

I'm trying to recognize image contours using OpenCV4 with Android Studio(kotlin). However, I don't know how to get xpos and ypos value from MatOfPoint variables. ( MatOfPoint variables is output of Imgproc.FindContours ) Do you have any solutions ?

private fun calcPosXY(){
    val bitmap = textureView.getBitmap()
    var imageMat = Mat()
    val contours = ArrayList<MatOfPoint>()
    val hierarchy = Mat()

    Utils.bitmapToMat(bitmap, imageMat)
    Imgproc.cvtColor(imageMat,imageMat,Imgproc.COLOR_RGB2GRAY) 
    Imgproc.threshold(imageMat, imageMat, 100.0, 255.0, Imgproc.THRESH_BINARY)
    Imgproc.findContours(imageMat, contours, hierarchy, Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_TC89_L1)

    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() //this variable
        approxCurve.convertTo(points,CvType.CV_8UC4)

        //
        // how to get xpos and ypos value from each points?
        //  e.g. xpos=327  ypos= 512 
        // 

    }
}