B4J Question jopencv, b4j, converting to an array

trying to change video0,video1, backsub0,backsub1 into arrays. appears to me that OCVBackgroundSubtractorMOG2 can't be converted to an array. can someone read the error message i get when i run the program in debug mode. confirm i'm right, maybe explain a work around. maybe my sytax is wrong ?
in case i'm not clear, i have:
Dim Video0 As OCVVideo
Dim Video1 As OCVVideo
Dim BackSub0 As OCVBackgroundSubtractorMOG2 = Video0.createBackgroundSubtractorMOG2(10,98,False)
Dim BackSub1 As OCVBackgroundSubtractorMOG2 = Video1.createBackgroundSubtractorMOG2(10,98,False)
i wish to have:
dim Videos(2) as OCVVideo
dim BackSubs(2) As OCVBackgroundSubtractorMOG2


arrays:
    Dim Video0 As  OCVVideo
    Dim Video1 As OCVVideo
   
    Dim BackSub0 As OCVBackgroundSubtractorMOG2 = Video0.createBackgroundSubtractorMOG2(10,98,False)
    Dim BackSub1 As OCVBackgroundSubtractorMOG2 = Video1.createBackgroundSubtractorMOG2(10,98,False)
   
   
    Dim NumOfTripWires As Int = 3
    Dim Videos(NumOfTripWires) As OCVVideo
    Dim WireCounter As Int = 0
    Do Until WireCounter = NumOfTripWires
        Dim BackSubs(WireCounter) As OCVBackgroundSubtractorMOG2 = Videos(WireCounter).createBackgroundSubtractorMOG2(10,98,False)
        WireCounter = WireCounter + 1
    Loop

error is this line:
Dim BackSubs(WireCounter) As OCVBackgroundSubtractorMOG2 = Videos(WireCounter).createBackgroundSubtractorMOG2(10,98,False)
this is error message:
Compiling generated Java code. Error
B4J line: 183
Dim BackSubs(WireCounter) As OCVBackgroundSubtra
src\b4j\example\main.java:289: error: incompatible types: BackgroundSubtractorMOG2 cannot be converted to BackgroundSubtractorMOG2[]
_backsubs = (com.b4jcv.video.BackgroundSubtractorMOG2[])(_videos[_wirecounter].createBackgroundSubtractorMOG2((int) (10),98,anywheresoftware.b4a.keywords.Common.False));
^
1 error
 
Last edited:

JordiCP

Expert
Licensed User
Longtime User
With this line
B4X:
    Dim BackSubs(WireCounter) As OCVBackgroundSubtractorMOG2 = Videos(WireCounter).createBackgroundSubtractorMOG2(10,98,False)
you were declaring an OCVBackgroundSubtractorMOG2 array of different length each time and assigning it to a single OCVBackgroundSubtractorMOG2 Object, which is programmatically incorrect.

The correct syntax should be
B4X:
    Dim NumOfTripWires As Int = 3
    Dim Videos(NumOfTripWires) As OCVVideo
    Dim BackSubs(NumOfTripWires) As OCVBackgroundSubtractorMOG2        ' Declare the array once
    Dim WireCounter As Int = 0
    Do Until WireCounter = NumOfTripWires
        BackSubs(WireCounter) = Videos(WireCounter).createBackgroundSubtractorMOG2(10,98,False)       ' and assign each one of the elements in the Do-Until Loop
        WireCounter = WireCounter + 1
    Loop

----- EDIT ------

Well, the above will fail because there is a bug in the current jOpenCV library which will throw an error when declaring an OCVBackgroundSubtractorMOG2 object
B4X:
Dim BackSubs(NumOfTripWires) As OCVBackgroundSubtractorMOG2         ' This will fail, either declaring a single item or an array, because a bug in the lib

A temporary workaround could be (the only constraint is that NumOfTripWires must be known at code time)
B4X:
   Dim NumOfTripWires As Int = 3
   Dim Videos(NumOfTripWires) As OCVVideo
   Dim BackSubs(NumOfTripWires) As OCVBackgroundSubtractorMOG2 = Array as OCVBackgroundSubtractorMOG2( _
      Videos(0).createBackgroundSubtractorMOG2(10,98,False), _
      Videos(1).createBackgroundSubtractorMOG2(10,98,False), _
      Videos(2).createBackgroundSubtractorMOG2(10,98,False) _
   )

Also note that you don't need to declare Videos(3). You can declare just one and then create the backgroundSubstractors from the same instance
B4X:
   Dim NumOfTripWires As Int = 3
   Dim Video As OCVVideo
   Dim BackSubs(NumOfTripWires) As OCVBackgroundSubtractorMOG2 = Array as OCVBackgroundSubtractorMOG2( _
      Video.createBackgroundSubtractorMOG2(10,98,False), _
      Video.createBackgroundSubtractorMOG2(10,98,False), _
      Video.createBackgroundSubtractorMOG2(10,98,False) _
   )
 
Last edited:
Upvote 0
i only need one video, very kool. look like the work around will work for me, i know the number of tripwires ahead of time. i'll give it a try and get back with the results shortly
 
Upvote 0
Top