gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:10 25 4
gpt4 key购买 nike

在横向模式下,我使用 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())

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

处理完 matToBitmap(myMAT, newBitmap)myImageView.setImageBitmap(newBitmap)

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

最佳答案

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

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

关于java - 从 imageview 触摸点,您可以调整 MAT 的大小以获得该点的颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48217071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com