Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, if I understood you correctly, you want to be able to touch some rectangle which is displayed on the screen and know programmatically if you touched inside it?

I hope that you know the coordinates of the rectangles points, otherwise it is a whole different problem!

Answering:

Your Activity has to implement OnTouchListener.

/** Tell your SurfaceView 'mOpenCvCameraView',   
* that your Activity 'MainActivity' is listening touch events   
* (you should place this on onManagerConnected() like in OpenCV samples):
*/
mOpenCvCameraView.setOnTouchListener(MainActivity.this);

/** Assumes the video is displayed full-screen  
*   mRgba is your input video frame  
*/
public boolean onTouch(View arg0, MotionEvent arg1) {
   // Fill and get the mask
   Point touchPoint = getImageCoordinates(getWindowManager(), mRgba, arg1.getX(), arg1.getY());

   // you should place the 4 corners of the rectangle here:
   MatOfPoint2f rectangle = new MatOfPoint2f(new Point(x1,y1), etc...);

   // Checks if touchPoint is inside rectangle
   // assumes rectangle is properly set.
   boolean result = Imgproc.pointPolygonTest(rectangle, touchPoint, false);

   return true;
}

/**
* Method to scale screen coordinates to image coordinates,   
* as they have different resolutions. 
*/
public static Point getImageCoordinates(WindowManager wManager, Mat image, float displayX, float displayY){
    Display display = wManager.getDefaultDisplay();     
    android.graphics.Point outSize = new android.graphics.Point();
    display.getSize(outSize);

    float xScale = outSize.x / (float) image.width();
    float yScale = outSize.y / (float) image.height();                  

    return new Point(displayX/xScale, displayY/yScale);
}

Yes, if I understood you correctly, you want to be able to touch some rectangle which is displayed on the screen and know programmatically if you touched inside it?

I hope that you know the coordinates of the rectangles points, otherwise it is a whole different problem!

Answering:

Your Activity has to implement OnTouchListener.

/** Tell your SurfaceView 'mOpenCvCameraView',   
* that your Activity 'MainActivity' is listening touch events   
* (you should place this on onManagerConnected() like in OpenCV samples):
*/
mOpenCvCameraView.setOnTouchListener(MainActivity.this);

/** Assumes the video is displayed full-screen  
*   mRgba is your input video frame  
*/
public boolean onTouch(View arg0, MotionEvent arg1) {
   // Fill and get the mask
    Point touchPoint = getImageCoordinates(getWindowManager(), mRgba, arg1.getX(), arg1.getY());

   // you should place the 4 corners of the rectangle here:
   MatOfPoint2f rectangle = new MatOfPoint2f(new Point(x1,y1), etc...);

   // Checks if touchPoint is inside rectangle
   // assumes rectangle is properly set.
   boolean result = Imgproc.pointPolygonTest(rectangle, touchPoint, false);

   return true;
}

/**
* Method to scale screen coordinates to image coordinates,   
* as they have different resolutions. 
*/
public static Point getImageCoordinates(WindowManager wManager, Mat image, float displayX, float displayY){
    Display display = wManager.getDefaultDisplay();     
    android.graphics.Point outSize = new android.graphics.Point();
    display.getSize(outSize);

    float xScale = outSize.x / (float) image.width();
    float yScale = outSize.y / (float) image.height();                  

    return new Point(displayX/xScale, displayY/yScale);
}

Yes, if I understood you correctly, you want to be able to touch some rectangle which is displayed on the screen and know programmatically if you touched inside it?

I hope that you know the coordinates of the rectangles points, otherwise it is a whole different problem!

Answering:

Your Activity has to implement OnTouchListener.You can do something like this:

/** Tell your SurfaceView 'mOpenCvCameraView',   
* that your Activity 'MainActivity' is listening touch events   
* (you should place this on onManagerConnected() like in OpenCV samples):
*/
mOpenCvCameraView.setOnTouchListener(MainActivity.this);

/** Assumes the video is displayed full-screen  
*   mRgba is your input video frame  
*/
public boolean onTouch(View arg0, MotionEvent arg1) for(int i = 0; i<contours.size(); i++) {

   Point touchPoint = getImageCoordinates(getWindowManager(), mRgba, arg1.getX(), arg1.getY());

   // you should place the 4 corners of the rectangle here:
   MatOfPoint2f rectangle = new MatOfPoint2f(new Point(x1,y1), etc...);

   // Checks if touchPoint is inside rectangle
   // assumes rectangle is properly set.
   boolean result = Imgproc.pointPolygonTest(rectangle, touchPoint, false);

   return true;
}

/**
* Method to scale screen coordinates to image coordinates,   
* as they have different resolutions. 
*/
public static Point getImageCoordinates(WindowManager wManager, Mat image, float displayX, float displayY){
 Display display MatOfPoint m = wManager.getDefaultDisplay(); contours.get(i);
  List<Point> list = m.toList();
 android.graphics.Point outSize for(int j = new android.graphics.Point();
    display.getSize(outSize);

    float xScale = outSize.x / (float) image.width();
    float yScale = outSize.y / (float) image.height(); 0; j<list.size(); j++) {
 Point p = list.get(j);
  if(p.x == 500){
 return new Point(displayX/xScale, displayY/yScale);
    }
    }
}

You can do something like this:

//  List<MatOfPoint> contours; from findCountours  

for(int i = 0; i<contours.size(); i++) {
    MatOfPoint m = contours.get(i);
    List<Point> list = m.toList();
    for(int j = 0; j<list.size(); j++) {
        Point p = list.get(j);
        if(p.x == 500){

        }
    }
}