简体   繁体   中英

Android + OpenCV: OnTouch getX(), getY() doesn't return right values

I am using OpenCV4Android FaceDetection sample. I have global table with rectangles of currently spoted faces:

private Rect[] facesArray;

also global floats to store onClick coordinates,

private float onClickX;
private float onClickY;

which are generated from onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent e) {

    onClickX = e.getX();
    onClickY = e.getY();

    switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // something not important for this matter happens here 
    }

    return super.onTouchEvent(e);
}

In onCameraFrame method before returning mat with view I am doing:

Core.circle(mRgba,new Point(onClickX,onClickY), 40,  new Scalar(0, 255, 0, 255));

So what happens. I draw a small, green circle on coordinates that are fetched in onTouchEvent and sent to global variables. Those variables (onClickX, onClickY) are read by onCameraFrame and used for core.circle() function. My problem is that, circle isn't drawn precisely under my finger but always in lower-right place. This is what happens: https://dl.dropboxusercontent.com/u/108321090/device-2013-07-24-004207.png

And this circle is always in the same direction/position to my finger whenever it is on screen, and meets it in top-left corner, dissapears on bottom right corner (goes outside screen). I tried using getRawX, geyRawY -> result is the same, I don't understeand why get commands doesn't return precise tap position but somewhere near it. I have no clue how to fix this.

This is the solution to your problem.

which is,

@Override
public boolean onTouch(View arg0,MotionEvent event) {

double cols = mRgba.cols();// mRgba is your image frame
double rows = mRgba.rows();

double xOffset = (mOpenCvCameraView.getWidth() - cols) / 2;
double yOffset = (mOpenCvCameraView.getHeight() - rows) / 2;

onClickX = (float)(event).getX() - xOffset;
onClickY = (float)(event).getY() - yOffset;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM