OpenCV4Android Sample App - what does this code snippet do?

asked 2016-01-08 03:41:37 -0600

Solace gravatar image

What is happening from line #129 to line #133 in this class of the Color blob detection sample app?

SOME CONTEXT:

The camera view in the app looks like this: (Notice that in the camera view, there is a black border around the camera frame)

image description

From Line 114 to 128, the following is happening.

  1. int cols = mRgba.cols(); cols() gives the number of columns in a matrix. The matrix here is the Mat representing a frame in the live stream of frames being displayed (and not the entire camera view), i.e. it represents the part of the camera view where live stream is being displayed, EXCLUDING the black border of the camera view.

  2. int rows = mRgba.rows(); rows() gives the number of rows in the camera frame, EXCLUDING the black border of the camera view.

  3. int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2; int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2; mOpenCvCameraView.getWidth() gives the number of rows in the entire camera view, i.e. the camera frame PLUS the black border of the camera view around the frame. (mOpenCvCameraView.getWidth() - cols) gives the sum of the width of left and right black border of the camera view. (mOpenCvCameraView.getWidth() - cols)/2 or xOffset gives the width of the black border on one side, i.e. either right or left side, black border of the camera view. Likewise for yOffset

  4. int x = (int)event.getX() - xOffset; int y = (int)event.getY() - yOffset; getX() returns the X coordinate of this event for the first pointer index. So getX() gives the distance of the touched region from the left extreme side of the camera view, which includes the black border on the left. So event.getX()-xOffset or int x is the distance of the touched region from the left extreme side of the camera "frame" (which does NOT include the black border of the camera view). Likewise for int y.

Then are the lines which I have no clue about.

edit retag flag offensive close merge delete

Comments

it's probably helpful, if you continue in the same way as above , - only with the lines you *don't * understand ?

berak gravatar imageberak ( 2016-01-08 04:02:27 -0600 )edit

129 - 133 basically construct an 8x8 square, with the touch point x,y as center.

also this takes care, not to go over the bounds of the image, if you tapped closer than 4 pixels to the border

berak gravatar imageberak ( 2016-01-08 06:00:19 -0600 )edit