Android Question Ellipse detection in an image with OpenCV

Discussion in 'Android Questions' started by Toky Olivier, Aug 3, 2019.

  1. Toky Olivier

    Toky Olivier Member Licensed User

    Hello all,
    I tried to detect circles/ellipse as done here: https://stackoverflow.com/questions/26087035/detecting-circles-in-binary-image/26125304#26125304 with this good very library: OpenCV (wrapped by @JordiCP: ) but I cannot get the perimeter of each contour. Areas, I can get them.
    The function arcLength exists but it requested OCVMatOfPoint2f for the first argument. It seems that contours.Get(i) return a OCVMatOfPoint not OCVMatOfPoint2f. So I cannot get it working if someone can help.

    My Code is:
    Code:
    Sub ProcessImage
        srcMat.copyTo( tmpMat)                                                        
    ' Clone source
        dstBitmap.InitializeMutable(tmpMat.size.width,tmpMat.size.height)
      
        
    Dim edge As OCVMat
        
    Dim contours As List
        
    Dim hierarchy As OCVMat
        
    Dim offset As OCVPoint
        
    Dim i As Int
        mImgProc.cvtColor1(tmpMat,tmpMat,mImgProc.COLOR_RGBA2GRAY)
        mImgProc.Canny3(tmpMat,edge,
    50,150)
        offset.Initialize(
    00)
        contours.Initialize
        mImgProc.findContours(edge, contours, hierarchy, mImgProc.RETR_EXTERNAL, mImgProc.CHAIN_APPROX_NONE, offset)
        
    Dim myColorScalar As OCVScalar
        myColorScalar.Initialize3(
    0,0,255)
        
    For i=0 To contours.Size-1
            
    'Dim curve As OCVMatOfPoint2f
            'Dim mat As OCVMatOfPoint = contours.Get(i)
            'curve.Initialize2(mat)
            Dim p As Double = 0 'mImgProc.arcLength(contours.Get(i), False)
            Dim a As Double = mImgProc.contourArea(contours.Get(i), False)
            
    Log("area: " & a & " perimeter: " & p)
            
    'If Abs(1-p*p/(4*3.1413*a))<0.2 Then
                mImgProc.drawContours1(srcMat, contours, i, myColorScalar, 2)
            
    'End If
        Next
      
        mUtils.matToBitmap1(srcMat,dstBitmap)
        dstIV.Bitmap = dstBitmap

    End Sub
    Please see enclosed the project.

    Objective:
    oe1B1.png

    It can detect all contours as you can see here:
    screenshot.png


    Thank you so much
     

    Attached Files:

    Last edited: Aug 3, 2019
  2. Toky Olivier

    Toky Olivier Member Licensed User

    Finally I solved it:
    Code:
    Sub ProcessImage   
        
    Dim edge As OCVMat
        
    Dim contours As List
        
    Dim hierarchy As OCVMat
        
    Dim offset As OCVPoint
        
    Dim i As Int
        
    Dim p As Double, a As Double
        srcMat.copyTo( tmpMat)                                                        
    ' Clone source
        dstBitmap.InitializeMutable(tmpMat.size.width,tmpMat.size.height)
        
        mImgProc.cvtColor1(tmpMat,tmpMat,mImgProc.COLOR_RGBA2GRAY)
        mImgProc.Canny3(tmpMat,edge,
    50,150)
        offset.Initialize(
    00)
        contours.Initialize
        mImgProc.findContours(edge, contours, hierarchy, mImgProc.RETR_EXTERNAL, mImgProc.CHAIN_APPROX_NONE, offset)
        
    Dim myColorScalar As OCVScalar
        myColorScalar.Initialize3(
    255,0,255)
        
    For i=0 To contours.Size-1
            
    Dim mat As OCVMatOfPoint = contours.Get(i)
            
    Dim pnt() As OCVPoint = mat.toArray
            
    Dim mat2 As OCVMatOfPoint2f
            mat2.Initialize3(pnt)
            p = mImgProc.arcLength(mat2, 
    False)
            a = mImgProc.contourArea(contours.Get(i), 
    False)
            
    Log("area: " & a & " perimeter: " & p)
            
    If Abs(1-p*p/(4*3.1413*a))<0.2 Then
                mImgProc.drawContours1(srcMat, contours, i, myColorScalar, 
    2)
            
    End If
        
    Next

        
        mUtils.matToBitmap1(srcMat,dstBitmap)
        dstIV.Bitmap = dstBitmap
    End Sub
    Screenshot:
    screenshot.png

    Attached the project:
     

    Attached Files:

    JordiCP, Erel and inakigarm like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice