Ask Your Question
0

How to get xpos and ypos value using FindContours

asked 2019-05-28 06:09:00 -0600

fjmkt gravatar image

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 
        // 

    }
}
edit retag flag offensive close merge delete

Comments

this is for sure wrong: approxCurve.convertTo(points,CvType.CV_8UC4) (convertTo does not handle channels, and you probably did not want to truncate coords to [0..255])

berak gravatar imageberak ( 2019-05-28 06:37:15 -0600 )edit

please write your code

fjmkt gravatar imagefjmkt ( 2019-05-29 02:48:18 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-05-28 06:52:33 -0600

berak gravatar image

have a look at the docs , MatOfPoint has toList and toArray access functions, you can also use a simple:

double[] d = points.get(i,0);
double x = d[0], y = d[1];
edit flag offensive delete link more

Comments

Thank you !

fjmkt gravatar imagefjmkt ( 2019-05-30 21:28:16 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-05-28 06:09:00 -0600

Seen: 144 times

Last updated: May 28 '19