0

I am working on my semester project. I want to make exactly the same thing as in this video. https://www.youtube.com/watch?v=_4hA36nXRjw&feature=youtu.be . I want to make a obstacle detection application in Android using OpenCV. I am unable to calculate the optical flow (By using function Video.calcOpticalFlowPyrLK) of the keypoints which I get from FeatureDetector Function. Further more I can't refine the points through RANSAC algorithm. I am in very much trouble. Kindly help me if anyone can. I shall be highly thankful to you.

public class MainActivity extends Activity implements CvCameraViewListener2 {

private static final String    TAG = "ziadkhan.optical_flow";

public static final int        VIEW_MODE_KLT_TRACKER=0;
public static final int        VIEW_MODE_OPTICAL_FLOW=1;

private int                    mViewMode;
private Mat                    mRgba;
private Mat                    mGray;
private Mat                    mIntermediateMat;
private Mat                    mPrevGray;
private CameraBridgeViewBase   mOpenCvCameraView;

MatOfPoint2f                   prevFeatures, nextFeatures;
MatOfPoint                     features;
private MatOfByte              status;
private MatOfFloat             err;
private MenuItem               mItemPreviewOpticalFlow, mItemPreviewKLT;

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch(status){
            case LoaderCallbackInterface.SUCCESS:
            {
                Log.i(TAG,"OpenCV Loaded Successfully");
                mOpenCvCameraView.enableView();
                break;
            }
            default:
            {
                super.onManagerConnected(status);
            }
        }
    }
};
//private JavaCameraView mOpenCvCameraView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.main_activity_surface_view);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
   // getMenuInflater().inflate(R.menu.menu_main, menu);
    mItemPreviewKLT = menu.add("KLT Tracker");
    mItemPreviewOpticalFlow = menu.add("Optical Flow");
    return true;
}

public void onResume() {
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mLoaderCallback);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item == mItemPreviewOpticalFlow) {
        mViewMode = VIEW_MODE_OPTICAL_FLOW;
        resetVars();
    }
    else if (item == mItemPreviewKLT){
        mViewMode = VIEW_MODE_KLT_TRACKER;
        resetVars();
    }

    return true;
}

private void resetVars() {
    mPrevGray = new Mat (mGray.rows(), mGray.cols(), CvType.CV_8UC1);
    features = new MatOfPoint();
    prevFeatures = new MatOfPoint2f();
    nextFeatures = new MatOfPoint2f();
    status = new MatOfByte();
    err = new MatOfFloat();
}

public void onDestroy(){
   super.onDestroy();
    if(mOpenCvCameraView != null){
        mOpenCvCameraView.disableView();
    }
}

@Override
public void onCameraViewStarted(int width, int height) {
    mRgba = new Mat(height, width, CvType.CV_8SC4);
    mIntermediateMat = new Mat(height, width, CvType.CV_8SC4);
    mGray = new Mat(height, width, CvType.CV_8SC1);
    resetVars();
}

@Override
public void onCameraViewStopped() {
    mRgba.release();
    mGray.release();
    mIntermediateMat.release();
}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    mGray = inputFrame.gray();
    final int viewmode = mViewMode;
    switch (viewmode) {
        case VIEW_MODE_OPTICAL_FLOW:

            if (features.toArray().length == 0) {
                int rowStep = 50, colStep = 100;
                int nRows = mGray.rows()/rowStep, nCols = mGray.cols()/colStep;

                Point points[] = new Point[nRows * nCols];
                for (int i = 0; i < nRows; i++) {
                    for (int j = 0; j < nCols; j++) {
                        points[i * nCols + j] = new Point(j * colStep, i * rowStep);
                    }
                }
                features.fromArray(points);
                prevFeatures.fromList(features.toList());
                mPrevGray = mGray.clone();
                break;
            }
            nextFeatures.fromArray(prevFeatures.toArray());
            Video.calcOpticalFlowPyrLK(mPrevGray, mGray, prevFeatures, nextFeatures, status, err);
            List<Point> prevList = features.toList(),
                    nextList = nextFeatures.toList();
            Scalar color = new Scalar(255);
            for (int i = 0; i < prevList.size(); i++) {
                Imgproc.line(mGray, prevList.get(i), nextList.get(i), color);
            }
            mPrevGray = mGray.clone();
            break;
        default:
            mViewMode = VIEW_MODE_OPTICAL_FLOW;
            break;
     }
    return mGray;
    }
}`
m5khan
  • 2,667
  • 1
  • 27
  • 37
Zain Ali
  • 9
  • 6
  • It won't be easy. https://xkcd.com/1425/ – jkdev Jun 28 '16 at 03:31
  • Then what should i do? – Zain Ali Jun 28 '16 at 03:36
  • Have a conversation with your professor or instructor. Talk about what you're thinking of doing, and ask how you should proceed. – jkdev Jun 28 '16 at 03:38
  • Also, welcome to Stack Overflow! We're glad to have you here. You should take the [site tour](https://stackoverflow.com/tour) and read up on [how to ask questions](https://stackoverflow.com/help/asking). Then feel free to post if you have a specific programming question that hasn't been asked before on the site. – jkdev Jun 28 '16 at 03:42
  • How far along are you in the project? Is there specific code that is causing the problem? – jkdev Jun 28 '16 at 03:48
  • Thankx Sir. By the way i am working on this project for last 5 months but i am not successful. I am really worried coz its my Final Year Project and my Degree completion is pending on this project. – Zain Ali Jun 28 '16 at 04:04
  • Sir i have added the video on my youtube channel and also i pasted the link above. Kindly see that Video. Thats exactly what i want to make. – Zain Ali Jun 28 '16 at 04:05
  • Yes. A specific problem is that i want to convert the keypoints extracted through function "Featuredetector" are of type MATOFPOINT and i want to caonvert them to MATOFPOINT2F as to calculate the optical flow it requires MATOFPOINT2F. – Zain Ali Jun 28 '16 at 04:07
  • This question might help: [OpenCV4Android conversion from MatOfKeyPoint to MatOfPoint2f](http://stackoverflow.com/q/14346482/3345375) – jkdev Jun 28 '16 at 04:12
  • I have folllowed this link but could not understand properly how to code it. I am not successful so far. I have done so much research but not successful. – Zain Ali Jun 28 '16 at 04:15
  • Sir kindly see this video. https://www.youtube.com/watch?v=_4hA36nXRjw&feature=youtu.be I actually want to make exactly same application like this. – Zain Ali Jun 28 '16 at 04:16
  • [This answer](http://stackoverflow.com/a/14402326/3345375) links to another page, http://www.answers.opencv.org/question/6206/opencv4android-conversion-from-matofkeypoint-to-matofpoint2f/ (although a Stack Overflow answer is really supposed to include the actual solution, and link-only answers are discouraged). – jkdev Jun 28 '16 at 04:18
  • can we chat in private Sir? I want to send you my code so that if you can understand my problem easily. – Zain Ali Jun 28 '16 at 04:22
  • If you have code then please do include it in the question itself -- that's often the most important part of the question. You can create a code block in your post by indenting text four spaces. – jkdev Jun 28 '16 at 04:23
  • (And I should mention that I personally don't know anything about Android development. I'm just one of the people who volunteers to review new Stack Overflow questions.) – jkdev Jun 28 '16 at 04:25
  • Actually i am following a book "Mastering OpenCV Android Application Programming". I learnt from there Optical flow and also i run the code present in that book that worked absolutely fine. But in that example the author take points on screen rather than passing the MatOfkeypoints extracted through FeatureDetector Fucntion. – Zain Ali Jun 28 '16 at 04:28
  • I am really gratefull to you that u replied and helped me. I am really thankful to you Sir. – Zain Ali Jun 28 '16 at 04:29
  • You're very welcome! And please feel free to post on Stack Overflow if you have a specific question about the code you are working with. – jkdev Jun 28 '16 at 04:32
  • Best of luck to you on your project! – jkdev Jun 28 '16 at 04:33
  • Thankx Sir. I will post my questions on Stack Overflow if i had any problem. Thankx – Zain Ali Jun 28 '16 at 04:38

0 Answers0