In terms of point of view, can you resize the MAT to get the color of the dots?

The landscape I use myImageView.setImageBitmap(myBitmap)and use a listener ontouchfor getXand getYand tune to the location myImageView(the same as getRawXand getRawY). Then I use bitmapToMatto create MAT for image processing using OpenCV. I found two resizing scenarios in which the location ontouchwould draw a circle exactly where I touched, but from time to time the location would be outside the Mat and cause NPE and fail during processing.

Scenario 1: resize(myImageView.getWidth(), myImageView.getHeight())

Scenario 2: resize(myImageView.getHeight(), myImageView.getWidth())and

x = x(myImage.getHeight()/myImageView.getWidth())

y = y(myImage.getWidth()/myImageView.getHeight())

If I don't change x, y, I can click throughout the image without NPE, but the circle was nowhere near where I touched.

After processing I matToBitmap(myMAT, newBitmap)and myImageView.setImageBitmap(newBitmap).

I am clearly missing something, but is there an easy way to get the contact location and use it in MAT? Any help would be awesome!

+6
source share
1 answer

You must compensate for the coordinates involved, as the view may be larger or smaller than the mat. Something like this should work

private Scalar getColor(View v, MotionEvent event){
    int cols = yourMat.cols();
    int rows = yourMat.rows();

    int xOffset = (v.getWidth() - cols) / 2;
    int yOffset = (v.getHeight() - rows) / 2;

    int x = (int)event.getX() - xOffset;
    int y = (int)event.getY() - yOffset;

  Point  touchedPoint    = new Point(x,y);

  Rect   touchedRect = new Rect();

    touchedRect.x = (x>4) ? x-4 : 0;
    touchedRect.y = (y>4) ? y-4 : 0;

    touchedRect.width = (x+4 < cols) ? x + 4 - touchedRect.x : cols - touchedRect.x;
    touchedRect.height = (y+4 < rows) ? y + 4 - touchedRect.y : rows - touchedRect.y;

    Mat touchedRegionRgba = yourMat.submat(touchedRect);

    Scalar mBlobColor = Core.mean(touchedRegionRgba);

    touchedRegionRgba.release();

    return mBlobColor;
}
+2
source

All Articles