【问题标题】:From an imageview touch point can you resize a MAT to get that points color?您可以从 imageview 接触点调整 MAT 的大小以获取该点的颜色吗?
【发布时间】:2018-06-21 08:57:50
【问题描述】:

在横向中,我使用myImageView.setImageBitmap(myBitmap)ontouch 监听器来监听getXgetY 并调整myImageView 的位置(与getRawXgetRawY 相同)。然后我使用bitmapToMat 构建一个 MAT 以使用 OpenCV 更多地处理图像。我发现了两种调整大小的场景,onTouch 位置会在我触摸的位置画一个圆圈,但有时该位置会在垫外并导致 NPE 并在处理过程中失败。

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

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

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

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

如果我不更改 x,y,我可以在没有 NPE 的情况下单击图像中的任何位置,但绘制的圆圈离我触摸的位置不远。

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

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

【问题讨论】:

  • 你能分享你的应用程序的屏幕截图吗? onTouch 回调的 x 和 y 坐标是 w.r.t ImageView,如果你的 Bitmap 没有占据整个 ImageView 那么你可能总是得到不正确的触摸坐标。

标签: java android opencv touch-event


【解决方案1】:

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

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多