从imageview触摸点,您可以调整MAT的大小以获得该点颜色吗?

Bri*_*Miz 6 java android opencv touch-event

在横向,我使用myImageView.setImageBitmap(myBitmap)和使用ontouch侦听器,以getXgetY和调整的位置myImageView(同getRawXgetRawY).然后bitmapToMat,我使用构建MAT来使用OpenCV更多地处理图像.我找到了两个调整大小的场景,其中onTouch位置将在我触摸的位置绘制一个圆圈,但有时位置将位于Mat之外并导致NPE并在处理期间失败.

场景1: resize(myImageView.getWidth(), myImageView.getHeight())

场景2:resize(myImageView.getHeight(), myImageView.getWidth())

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

y = y(myImage.getWidth()/myImageView.getHeight())
Run Code Online (Sandbox Code Playgroud)

如果我不改变x,我可以点击NPE图像中的任何地方,但画出的圆圈远不在我触摸的地方.

处理后我matToBitmap(myMAT, newBitmap)myImageView.setImageBitmap(newBitmap).

我显然错过了一些东西,但有没有一个简单的方法来获得触摸位置并在MAT中使用该位置?任何帮助都是极好的!

Niz*_*ale 2

您必须偏移触摸的坐标,因为视图可能比垫子更大或更小。像这样的东西应该有效

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;
}
Run Code Online (Sandbox Code Playgroud)