public boolean onTouch(View v, MotionEvent event) { int cols = mRgba.cols(); int rows = mRgba.rows(); int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2; int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2; int x = (int) event.getX() - xOffset; int y = (int) event.getY() - yOffset; Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")"); if ((x < 0) || (y < 0) || (x > cols) || (y > rows)) return false; 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 = mRgba.submat(touchedRect); Mat touchedRegionHsv = new Mat(); Imgproc.cvtColor(touchedRegionRgba, touchedRegionHsv, Imgproc.COLOR_RGB2HSV_FULL); // Calculate average color of touched region mBlobColorHsv = Core.sumElems(touchedRegionHsv); int pointCount = touchedRect.width * touchedRect.height; for (int i = 0; i < mBlobColorHsv.val.length; i++) mBlobColorHsv.val[i] /= pointCount; mBlobColorRgba = converScalarHsv2Rgba(mBlobColorHsv); Log.i( TAG, "Touched rgba color: (" + mBlobColorRgba.val[0] + ", " + mBlobColorRgba.val[1] + ", " + mBlobColorRgba.val[2] + ", " + mBlobColorRgba.val[3] + ")"); mDetector.setHsvColor(mBlobColorHsv); Imgproc.resize(mDetector.getSpectrum(), mSpectrum, SPECTRUM_SIZE); mIsColorSelected = true; final Handler handler = new Handler(); Runnable runable = new Runnable() { @Override public void run() { try { // do your code here hehe.setText(abc.toString()); if (abc > 16000 && abc < 25000) { imageView.setImageResource(R.drawable.green1); } else { imageView.setImageResource(R.drawable.red1); } // also call the same runnable handler.postDelayed(this, 1000); } catch (Exception e) { // TODO: handle exception } finally { // also call the same runnable // handler.postDelayed(this, 1000); } } }; handler.postDelayed(runable, 1000); touchedRegionRgba.release(); touchedRegionHsv.release(); return false; // don't need subsequent touch events }