How to convert android touch coordinates to OpenCV

2019-02-20 08:23发布

问题:

I am trying to do something simillar to this, but on Android: http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html I have an image drawn to ImageView and i want to be able to select 6 points (3 source and 3 target) and use the warpAffine method on OpenCV like the example before. I'm able to reproduce the example above, but when passing the X,Y coordinates from onTouch event, it doesn't give me what i expect. I think the problem is how to convert the android touch coordinates to OpenCV Mat column/row. Maybe i need to display the image on OpenCV surface? I hope i was clear, Thanks in advance.

回答1:

The touch coordinate refer to a point that belongs to your Android's screen resolution (Samsung Galaxy S3 is 720x1280). On the other hand, the OpenCV image could be larger or smaller than that, meaning that a touch coordinate can't be mapped directly into an image coordinate.

What needs to be done to convert from one resolution to the other, is the normalization of the touch coordinate from 720x1280 to the 640x480 range. Therefore, to find the real coordinates on the image you would do:

real_x = (touch_x * 640) / 720;
real_y = (touch_y * 480) / 1280;