In landscape orientation, I using myImageView.setImageBitmap(myBitmap)
and using an ontouch
listener to getX
and getY
and adjust for location of myImageView
(same as getRawX
and getRawY
). I then use bitmapToMat
to build a MAT to process the image more with OpenCV. I found two resizing scenarios where the onTouch
location will draw a circle exactly where I touched but at times the location will be outside the Mat and cause a 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 dont change the x,y I can click everywhere in image w/o the NPE but the circle drawn is nowhere near where I touched.
After processing I matToBitmap(myMAT, newBitmap)
and myImageView.setImageBitmap(newBitmap)
.
I am obviously missing something but is there there a simple way to get the touch location and use that location in a MAT? Any help would be awesome!
1 Answers
Answers 1
You have to offset the touched coordinates as the view might be bigger 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; }
0 comments:
Post a Comment