Android Example OpenCV real time car detection examples, by @moster67 (and friends)

This is a full working example of real time ObjectDetection (in this case, cars) using cascadeClassifiers, getting the input from a recorded video file, making use of Videocapture. The example was written by @moster67 who kindly gave me permission to publish it here. I have only tuned a couple of things ( :D ) , but the example is very good for learning as it makes use of several OpenCV classes.

This project can be used as a starting point for any other type of object detection: as in real-world applications, many things can be tuned: cascade classifier files, image conditioning, applying logic, ....
It is an excellent example to start experimenting. Also the code is both compact and commented.


A bit of explanation of cascade classifiers
http://docs.opencv.org/3.2.0/dc/d88/tutorial_traincascade.html

Link to the video of the original @moster67 post

The code
B4X:
#Region  Project Attributes
   #ApplicationLabel: Car Detection
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: landscape
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: true
   #IncludeTitle: false
#End Region

'** SAMPLE CODE TO DETECT CARS FROM A VIDEO FILE USING OPENCV FOR B4A  **
'** VIDEO MUST BE IN MJPEG AND WITHIN AN AVI CONTAINER (VIDEO INCLUDED) **
'** OPENCV WRAPPER BY @JordicCP, SAMPLE CODE BY @Moster67  **

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
  
   Dim ocl As OCVOpenCVLoader
  
   Dim mVideo As OCVVideoCapture
   Dim mFrame As OCVMat
   Dim mUtils As OCVUtils
   Dim mVio As OCVVideoio
   Dim mCasc As OCVCascadeClassifier
   Dim mImgProc As OCVImgproc
   Dim carsrect As OCVMatOfRect
   Dim graycarframe As OCVMat
   Dim mMinSize, mMaxSize As OCVSize
   Dim mRect() As OCVRect
   Dim mScalar As OCVScalar
  
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Private ImV As ImageView
   Dim mBmap As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
'   Activity.LoadLayout("1")
  
   'copying video file from Dir.Assets to Dir.Internal.
   'Probably not needed....
   If File.Exists(File.DirAssets, "cars3.avi") Then
     File.Copy(File.DirAssets, "cars3.avi",File.DirInternal,"cars3.avi")
     File.Copy(File.DirAssets, "cars.xml",File.DirInternal,"cars.xml")
   Else
     Log("Couldn't find a file. I am now exiting app")
     Activity.Finish
   End If
  
   mCasc.Initialize(File.DirInternal & "/cars.xml") 'loading a cascade classification file specific for cars.
   ImV.Initialize("ImV")
   ImV.Gravity = Gravity.FILL
   Activity.AddView(ImV,0,0,100%x,100%y)

   mVideo.Initialize(File.DirInternal & "/cars3.avi",mVio.CAP_ANY)
   Log(mVideo.isOpened)
  
   mFrame.Initialize 'mframe is a MatObject
   mBmap.InitializeMutable(520,520)
   ImV.Bitmap = mBmap
    
   Do While mVideo.isOpened 'looping through video to process each frame and display them
     If mVideo.read(mFrame) Then
      
       If mFrame.rows <> mBmap.Height Or mFrame.cols <> mBmap.Width Then
         mBmap.InitializeMutable(mFrame.cols,mFrame.rows)
         ImV.Bitmap = mBmap
        
         'Also rescale ImV layout so that it keeps the same aspect ratio as its content
         'We are working in ladscape. Will center horizontally in screen
         Dim aspectRatio As Float = mFrame.cols/mFrame.rows
         Dim delta As Int = (Activity.Width- 100%Y*aspectRatio)/2
         ImV.SetLayout( delta,0,100%Y*aspectRatio,100%Y)
       End If
      
       'image processing
       carsrect.Initialize
       mImgProc.cvtColor1(mFrame,graycarframe,mImgProc.COLOR_BGR2GRAY) 'convert the frame in gray scale
       mImgProc.equalizeHist(graycarframe,graycarframe) 'equalize the frame histogram to improve the result
      
       'detecting the cars
       mMinSize.Initialize2(Array As Double(0.0,0.0))
       mCasc.detectMultiScale(graycarframe, carsrect, 1.1, 1, 2, mMinSize, mMaxSize)
      
       'each rectangle in carsrect is a car: draw them
       mRect = carsrect.toArray
       mScalar.Initialize3(0,255,0)
       For i = 0 To carsrect.toArray.Length -1
         mImgProc.rectangle1(mFrame,mRect(i).tl,mRect(i).br, mScalar,1)
       Next
      
       'convert mat(=our frame) to bitmap and redraw imageview
       mUtils.matToBitmap1(mFrame,mBmap)
       ImV.Invalidate
      
       Sleep(0) 'needed to update the GUI. For versions of B4A earlier than 7.00, you can use DoEvents instead
     Else
       mVideo.release 'very important
     End If
    
   Loop
    
   Log("VIDEO ENDED")
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

As the example needs the cascade classifier file and the video, its size is too big to be uploaded.
You can find it in this shared link:
https://drive.google.com/drive/folders/0B-eBY3pamAunMUJFSXFHMFZSbGM?usp=sharing
 
Last edited:

moster67

Expert
Licensed User
Longtime User
It's very simple - the 2 video files you tried to load are encoded in mpeg4 and not MJPEG.

It has already been mentioned in other threads here in the forum that OpenCV for Android can only load video-files encoded in MJPEG within an AVI container while standard OpenCV can load other formats too. So even if you see the container as AVI, it does not mean that the video-file within is in MJPEG-format. As a matter of fact, probably most video-files included in OpenCV-examples which you can find on the internet/github are not in the correct codec/format.

You can easily check that using video-tools such as ffmpeg. If you for some reason need to convert a video-file to MJPEG you can do that on your PC using ffmpeg or even directly on the device using my ffmpeg-encoder library.
 

JordiCP

Expert
Licensed User
Longtime User
Thanks to an example code kindly shared by @cledic :), I made a small variation based on the original @moster67 example, in which this time a different principle is used to find moving objects.

The class used is OCVBackgroundSubstractorMOG2, and, as it name says (or not), is a simple yet powerful class which finds differences between frames in a time window, in which parameters as history depth and variation threshold can be set. The output of its main method is a mask where the 'points' that meet the variation criteria are set.

A static camera (I tried first examples using my device camera in my hand and it was just impossible, since it always moves 'a bit') or recorded video can be used as an input. It works real-time with no problems.

The modified code can make use of both techniques (the original cascadedetector and the backgroundsubstractor), and will switch from one to the other 'on the fly' when the user clicks on screen (just for demo purposes), as seen in the video

The new source, still quite compact
B4X:
#Region  Project Attributes
   #ApplicationLabel: Car Detection
   #VersionCode: 2
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: landscape
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: true
   #IncludeTitle: false
#End Region

'** SAMPLE CODE TO DETECT CARS FROM A VIDEO FILE USING OPENCV FOR B4A  **
'** VIDEO MUST BE IN MJPEG AND WITHIN AN AVI CONTAINER (VIDEO INCLUDED) **
'** OPENCV WRAPPER BY @JordicCP, SAMPLE CODE BY @Moster67  **

'*** Version code 2: added backgroundSubstractor method, thanks to @cledic ***

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim ocl As OCVOpenCVLoader
  
   Dim mVideoCapt As OCVVideoCapture
   Dim mFrame As OCVMat
   Dim mUtils As OCVUtils
   Dim mVio As OCVVideoio
   Dim mCasc As OCVCascadeClassifier
   Dim mImgProc As OCVImgproc
   Dim carsrect As OCVMatOfRect
   Dim graycarframe As OCVMat
   Dim mMinSize, mMaxSize As OCVSize
   Dim mRect() As OCVRect
   Dim mScalar As OCVScalar
  
   'For backgroundsubstractor....
   Dim mVideo As OCVVideo

   'This line must be declared and initted here due to a bug in V1.00 ;-P
   Dim mOCVBackgroundSubtractor As OCVBackgroundSubtractorMOG2 = mVideo.createBackgroundSubtractorMOG2(80,50,False)

   Dim fgMaskMOG2,fgMaskMOG3 As OCVMat
   Dim tmp As OCVMat
  
   Dim useSubstractor As Boolean = True
  
End Sub  
  
Sub Globals  
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Private ImV As ImageView
   Dim mBmap As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
'   Activity.LoadLayout("1")
  
   'copying video file from Dir.Assets to Dir.Internal.
   'Probably not needed....
   If File.Exists(File.DirAssets, "cars3.avi") Then
     File.Copy(File.DirAssets, "cars3.avi",File.DirInternal,"cars3.avi")
     File.Copy(File.DirAssets, "cars.xml",File.DirInternal,"cars.xml")
   Else
     Log("Couldn't find a file. I am now exiting app")
     Activity.Finish
   End If
  
   mCasc.Initialize(File.DirInternal & "/cars.xml") 'loading a cascade classification file specific for cars.
   ImV.Initialize("ImV")
   ImV.Gravity = Gravity.FILL
   Activity.AddView(ImV,0,0,100%x,100%y)

   mVideoCapt.Initialize1(File.DirInternal & "/cars3.avi",mVio.CAP_ANY)
   Log(mVideoCapt.isOpened)
  
   mFrame.Initialize 'mframe is a MatObject
   mBmap.InitializeMutable(520,520)
   ImV.Bitmap = mBmap
    
   Do While mVideoCapt.isOpened 'looping through video to process each frame and display them
     If mVideoCapt.read(mFrame) Then
      
       If mFrame.rows <> mBmap.Height Or mFrame.cols <> mBmap.Width Then
         mBmap.InitializeMutable(mFrame.cols,mFrame.rows)
         ImV.Bitmap = mBmap
        
         'Also rescale ImV layout so that it keeps the same aspect ratio as its content
         'We are working in ladscape. Will center horizontally in screen
         Dim aspectRatio As Float = mFrame.cols/mFrame.rows
         Dim delta As Int = (Activity.Width- 100%Y*aspectRatio)/2
         ImV.SetLayout( delta,0,100%Y*aspectRatio,100%Y)
       End If
      
If useSubstractor Then      
       mOCVBackgroundSubtractor.apply1(mFrame, fgMaskMOG2)      'feed the frame to the algorithm, and it will output a mask.
  
       ' Perform dilation and erosion to remove any small blobs left in the image
       Dim a As OCVMat
       Dim tmp As OCVMat
       mImgProc.erode2( fgMaskMOG2, tmp, a)
       mImgProc.dilate2(  tmp, tmp, a)

       ' A small variation for demo purposes. Draw the movement directly on the same image
       mImgProc.cvtColor1(tmp,fgMaskMOG3,mImgProc.COLOR_GRAY2RGB)  ' First we need to convert to the same format as original mat
       fgMaskMOG3.copyTo1(mFrame,fgMaskMOG2)                                 ' Then we copy it to the source, using itself (the detected areas) as a mask
Else           
       'image processing
       carsrect.Initialize
       mImgProc.cvtColor1(mFrame,graycarframe,mImgProc.COLOR_BGR2GRAY) 'convert the frame in gray scale
       mImgProc.equalizeHist(graycarframe,graycarframe) 'equalize the frame histogram to improve the result
      
       'detecting the cars
       mMinSize.Initialize2(Array As Double(0.0,0.0))
       mCasc.detectMultiScale(graycarframe, carsrect, 1.1, 1, 2, mMinSize, mMaxSize)
      
       'each rectangle in carsrect is a car: draw them
       mRect = carsrect.toArray
       mScalar.Initialize3(0,255,0)
       For i = 0 To carsrect.toArray.Length -1
         mImgProc.rectangle1(mFrame,mRect(i).tl,mRect(i).br, mScalar,1)
       Next
End If      
      
       'convert mat(=our frame) to bitmap and redraw imageview
       mUtils.matToBitmap1(mFrame,mBmap)
       ImV.Invalidate
      
       Sleep(0) 'needed to update the GUI. For versions of B4A earlier than 7.00, you can use DoEvents instead
     Else
       mVideoCapt.release 'very important
     End If
    
   Loop
    
   Log("VIDEO ENDED")
     
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub IMV_Click 
   useSubstractor=Not(useSubstractor)
End Sub

(in order to run this second example you'll need to download the first one from the link and copy-paste this one)
 

SoportePatrol

Member
Licensed User
Longtime User
Thanks to an example code kindly shared by @cledic :), I made a small variation based on the original @moster67 example, in which this time a different principle is used to find moving objects.

The class used is OCVBackgroundSubstractorMOG2, and, as it name says (or not), is a simple yet powerful class which finds differences between frames in a time window, in which parameters as history depth and variation threshold can be set. The output of its main method is a mask where the 'points' that meet the variation criteria are set.

A static camera (I tried first examples using my device camera in my hand and it was just impossible, since it always moves 'a bit') or recorded video can be used as an input. It works real-time with no problems.

The modified code can make use of both techniques (the original cascadedetector and the backgroundsubstractor), and will switch from one to the other 'on the fly' when the user clicks on screen (just for demo purposes), as seen in the video

The new source, still quite compact
B4X:
#Region  Project Attributes
   #ApplicationLabel: Car Detection
   #VersionCode: 2
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: landscape
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: true
   #IncludeTitle: false
#End Region

'** SAMPLE CODE TO DETECT CARS FROM A VIDEO FILE USING OPENCV FOR B4A  **
'** VIDEO MUST BE IN MJPEG AND WITHIN AN AVI CONTAINER (VIDEO INCLUDED) **
'** OPENCV WRAPPER BY @JordicCP, SAMPLE CODE BY @Moster67  **

'*** Version code 2: added backgroundSubstractor method, thanks to @cledic ***

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim ocl As OCVOpenCVLoader
 
   Dim mVideoCapt As OCVVideoCapture
   Dim mFrame As OCVMat
   Dim mUtils As OCVUtils
   Dim mVio As OCVVideoio
   Dim mCasc As OCVCascadeClassifier
   Dim mImgProc As OCVImgproc
   Dim carsrect As OCVMatOfRect
   Dim graycarframe As OCVMat
   Dim mMinSize, mMaxSize As OCVSize
   Dim mRect() As OCVRect
   Dim mScalar As OCVScalar
 
   'For backgroundsubstractor....
   Dim mVideo As OCVVideo

   'This line must be declared and initted here due to a bug in V1.00 ;-P
   Dim mOCVBackgroundSubtractor As OCVBackgroundSubtractorMOG2 = mVideo.createBackgroundSubtractorMOG2(80,50,False)

   Dim fgMaskMOG2,fgMaskMOG3 As OCVMat
   Dim tmp As OCVMat
 
   Dim useSubstractor As Boolean = True
 
End Sub
 
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Private ImV As ImageView
   Dim mBmap As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
'   Activity.LoadLayout("1")
 
   'copying video file from Dir.Assets to Dir.Internal.
   'Probably not needed....
   If File.Exists(File.DirAssets, "cars3.avi") Then
     File.Copy(File.DirAssets, "cars3.avi",File.DirInternal,"cars3.avi")
     File.Copy(File.DirAssets, "cars.xml",File.DirInternal,"cars.xml")
   Else
     Log("Couldn't find a file. I am now exiting app")
     Activity.Finish
   End If
 
   mCasc.Initialize(File.DirInternal & "/cars.xml") 'loading a cascade classification file specific for cars.
   ImV.Initialize("ImV")
   ImV.Gravity = Gravity.FILL
   Activity.AddView(ImV,0,0,100%x,100%y)

   mVideoCapt.Initialize1(File.DirInternal & "/cars3.avi",mVio.CAP_ANY)
   Log(mVideoCapt.isOpened)
 
   mFrame.Initialize 'mframe is a MatObject
   mBmap.InitializeMutable(520,520)
   ImV.Bitmap = mBmap
  
   Do While mVideoCapt.isOpened 'looping through video to process each frame and display them
     If mVideoCapt.read(mFrame) Then
    
       If mFrame.rows <> mBmap.Height Or mFrame.cols <> mBmap.Width Then
         mBmap.InitializeMutable(mFrame.cols,mFrame.rows)
         ImV.Bitmap = mBmap
      
         'Also rescale ImV layout so that it keeps the same aspect ratio as its content
         'We are working in ladscape. Will center horizontally in screen
         Dim aspectRatio As Float = mFrame.cols/mFrame.rows
         Dim delta As Int = (Activity.Width- 100%Y*aspectRatio)/2
         ImV.SetLayout( delta,0,100%Y*aspectRatio,100%Y)
       End If
    
If useSubstractor Then    
       mOCVBackgroundSubtractor.apply1(mFrame, fgMaskMOG2)      'feed the frame to the algorithm, and it will output a mask.
 
       ' Perform dilation and erosion to remove any small blobs left in the image
       Dim a As OCVMat
       Dim tmp As OCVMat
       mImgProc.erode2( fgMaskMOG2, tmp, a)
       mImgProc.dilate2(  tmp, tmp, a)

       ' A small variation for demo purposes. Draw the movement directly on the same image
       mImgProc.cvtColor1(tmp,fgMaskMOG3,mImgProc.COLOR_GRAY2RGB)  ' First we need to convert to the same format as original mat
       fgMaskMOG3.copyTo1(mFrame,fgMaskMOG2)                                 ' Then we copy it to the source, using itself (the detected areas) as a mask
Else         
       'image processing
       carsrect.Initialize
       mImgProc.cvtColor1(mFrame,graycarframe,mImgProc.COLOR_BGR2GRAY) 'convert the frame in gray scale
       mImgProc.equalizeHist(graycarframe,graycarframe) 'equalize the frame histogram to improve the result
    
       'detecting the cars
       mMinSize.Initialize2(Array As Double(0.0,0.0))
       mCasc.detectMultiScale(graycarframe, carsrect, 1.1, 1, 2, mMinSize, mMaxSize)
    
       'each rectangle in carsrect is a car: draw them
       mRect = carsrect.toArray
       mScalar.Initialize3(0,255,0)
       For i = 0 To carsrect.toArray.Length -1
         mImgProc.rectangle1(mFrame,mRect(i).tl,mRect(i).br, mScalar,1)
       Next
End If    
    
       'convert mat(=our frame) to bitmap and redraw imageview
       mUtils.matToBitmap1(mFrame,mBmap)
       ImV.Invalidate
    
       Sleep(0) 'needed to update the GUI. For versions of B4A earlier than 7.00, you can use DoEvents instead
     Else
       mVideoCapt.release 'very important
     End If
  
   Loop
  
   Log("VIDEO ENDED")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub IMV_Click
   useSubstractor=Not(useSubstractor)
End Sub

(in order to run this second example you'll need to download the first one from the link and copy-paste this one)


Hello everyone, the previous example has been very useful, thanks JordiCP.

Could someone be kind enough to tell me how to detect only large blobs, such as a door that opens or a person?

Well, there is the xml method but it would be very useful to get the variation of the first frame with respect to the current one, something like the "absdiff" method. Thank you!
 
Last edited:

BarryW

Active Member
Licensed User
Longtime User
This is a full working example of real time ObjectDetection (in this case, cars) using cascadeClassifiers, getting the input from a recorded video file, making use of Videocapture. The example was written by @moster67 who kindly gave me permission to publish it here. I have only tuned a couple of things ( :D ) , but the example is very good for learning as it makes use of several OpenCV classes.

This project can be used as a starting point for any other type of object detection: as in real-world applications, many things can be tuned: cascade classifier files, image conditioning, applying logic, ....
It is an excellent example to start experimenting. Also the code is both compact and commented.


A bit of explanation of cascade classifiers
http://docs.opencv.org/3.2.0/dc/d88/tutorial_traincascade.html

Link to the video of the original @moster67 post

The code
B4X:
#Region  Project Attributes
   #ApplicationLabel: Car Detection
   #VersionCode: 1
   #VersionName:
   'SupportedOrientations possible values: unspecified, landscape or portrait.
   #SupportedOrientations: landscape
   #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
   #FullScreen: true
   #IncludeTitle: false
#End Region

'** SAMPLE CODE TO DETECT CARS FROM A VIDEO FILE USING OPENCV FOR B4A  **
'** VIDEO MUST BE IN MJPEG AND WITHIN AN AVI CONTAINER (VIDEO INCLUDED) **
'** OPENCV WRAPPER BY @JordicCP, SAMPLE CODE BY @Moster67  **

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
 
   Dim ocl As OCVOpenCVLoader
 
   Dim mVideo As OCVVideoCapture
   Dim mFrame As OCVMat
   Dim mUtils As OCVUtils
   Dim mVio As OCVVideoio
   Dim mCasc As OCVCascadeClassifier
   Dim mImgProc As OCVImgproc
   Dim carsrect As OCVMatOfRect
   Dim graycarframe As OCVMat
   Dim mMinSize, mMaxSize As OCVSize
   Dim mRect() As OCVRect
   Dim mScalar As OCVScalar
 
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Private ImV As ImageView
   Dim mBmap As Bitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
'   Activity.LoadLayout("1")
 
   'copying video file from Dir.Assets to Dir.Internal.
   'Probably not needed....
   If File.Exists(File.DirAssets, "cars3.avi") Then
     File.Copy(File.DirAssets, "cars3.avi",File.DirInternal,"cars3.avi")
     File.Copy(File.DirAssets, "cars.xml",File.DirInternal,"cars.xml")
   Else
     Log("Couldn't find a file. I am now exiting app")
     Activity.Finish
   End If
 
   mCasc.Initialize(File.DirInternal & "/cars.xml") 'loading a cascade classification file specific for cars.
   ImV.Initialize("ImV")
   ImV.Gravity = Gravity.FILL
   Activity.AddView(ImV,0,0,100%x,100%y)

   mVideo.Initialize(File.DirInternal & "/cars3.avi",mVio.CAP_ANY)
   Log(mVideo.isOpened)
 
   mFrame.Initialize 'mframe is a MatObject
   mBmap.InitializeMutable(520,520)
   ImV.Bitmap = mBmap
   
   Do While mVideo.isOpened 'looping through video to process each frame and display them
     If mVideo.read(mFrame) Then
     
       If mFrame.rows <> mBmap.Height Or mFrame.cols <> mBmap.Width Then
         mBmap.InitializeMutable(mFrame.cols,mFrame.rows)
         ImV.Bitmap = mBmap
       
         'Also rescale ImV layout so that it keeps the same aspect ratio as its content
         'We are working in ladscape. Will center horizontally in screen
         Dim aspectRatio As Float = mFrame.cols/mFrame.rows
         Dim delta As Int = (Activity.Width- 100%Y*aspectRatio)/2
         ImV.SetLayout( delta,0,100%Y*aspectRatio,100%Y)
       End If
     
       'image processing
       carsrect.Initialize
       mImgProc.cvtColor1(mFrame,graycarframe,mImgProc.COLOR_BGR2GRAY) 'convert the frame in gray scale
       mImgProc.equalizeHist(graycarframe,graycarframe) 'equalize the frame histogram to improve the result
     
       'detecting the cars
       mMinSize.Initialize2(Array As Double(0.0,0.0))
       mCasc.detectMultiScale(graycarframe, carsrect, 1.1, 1, 2, mMinSize, mMaxSize)
     
       'each rectangle in carsrect is a car: draw them
       mRect = carsrect.toArray
       mScalar.Initialize3(0,255,0)
       For i = 0 To carsrect.toArray.Length -1
         mImgProc.rectangle1(mFrame,mRect(i).tl,mRect(i).br, mScalar,1)
       Next
     
       'convert mat(=our frame) to bitmap and redraw imageview
       mUtils.matToBitmap1(mFrame,mBmap)
       ImV.Invalidate
     
       Sleep(0) 'needed to update the GUI. For versions of B4A earlier than 7.00, you can use DoEvents instead
     Else
       mVideo.release 'very important
     End If
   
   Loop
   
   Log("VIDEO ENDED")
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

As the example needs the cascade classifier file and the video, its size is too big to be uploaded.
You can find it in this shared link:
https://drive.google.com/drive/folders/0B-eBY3pamAunMUJFSXFHMFZSbGM?usp=sharing
can we use this for realtime like webcams
 

JordiCP

Expert
Licensed User
Longtime User
can we use this for realtime like webcams
You can use any image source. Each input frame will need to be converted to bitmap and then process it.
Also, some parameters may need to be adjusted depending on preview size and/or frame rate.
 

roberto64

Active Member
Licensed User
Longtime User
Hi JordiCP, I'm giving an example where with the smartphone camera frame a soccer ball and the round one should appear, but I know that in the opencv library I can't find the cvRound class because?
regards
 
Top