0

I use ffmpeg to play video stream on SurfaceView of Android project. Now I would like to implement following feature. 1) Select one object by drawing a red rectangle on the SurfaceView. 2) Send x, y, width, height of the selected object and the original video frame to opencv. 3) Then, opencv return the new x and y of the object by processing the new video frame.

Anybody did it before? I will be very nice of you to give me some suggestion, or tell me very I can download the demo source code. Thank you so much.

Kame Li
  • 3
  • 6

1 Answers1

2

For part (1), try searching Google a little more. It won't be hard to find a tutorial that uses touch input, a tutorial to draw a rectangle, and a tutorial to draw over the SurfaceView. Part (2) is done just by how you set up and define your variables - there isn't a specific mechanism or function that "sends" the data over.

Part (3) is the part that isn't obvious, so that's the part I'll focus on. As with most problems in computer vision, you can solve object tracking in many ways. In no particular order, what comes to mind includes:

  • Optical Flow - Python openCV examples are here
    • Lucas-Kanade - the algorithm compares extracted features frame-by-frame. [The features are Shi-Tomasi by default, but can also be BRIEF, ORB, SIFT/SURF, or any others.] This runs quickly enough if the number of features is reasonable [within an order of magnitude of 100].
    • Dense [Farneback] - the algorithm compares consecutive frames and produces a dense vector field of motion direction and magnitude.
  • Direct Image Registration - if the motion between frames is small [about 5-15% of the camera's field of view], there are functions that can map the previous image to the current image quickly and efficiently. However, this feature is not in the vanilla OpenCV package - you need to download and compile the contrib modules, and use the Android NDK. If you're a beginner with Java and C++, I don't recommend using it.
  • Template Matching [example] - useful and cheap if the object and camera orientations do not change much.
Mark Miller
  • 706
  • 4
  • 14
  • Thanks very much for your answer. Part one and Part two are OK yesterday, and the problem is Part three. I need find a java sample code to process Part three now. Do you know where I can download the java sample code? Thanks a lot. ^_^ – Kame Li Jul 16 '15 at 03:11
  • Sorry, it seems that I cannot found anything by above link. In fact, the method "Template Matching" is the right one I need, but I just don't know how to convert them into java code. What I really want to do is to use Opencv4Android to track one object on the surfaceview, because I would like to implement this feature on android application. Thanks. – Kame Li Jul 16 '15 at 04:07
  • http://stackoverflow.com/questions/17001083/opencv-template-matching-example-in-android - once again, please try finding it on Google before you ask - it's a minimal amount of work. – Mark Miller Jul 16 '15 at 12:13
  • Sorry for the late reply. You answer is the right one what I need, thank you so much. – Kame Li Jul 22 '15 at 01:19
  • Glad it helped you! Click on the little check-mark to the left of my original answer to mark it as accepted. – Mark Miller Jul 22 '15 at 02:08