let's say, you've got a mask image of the same type and size of your image, a white square in a black image,
applying this works basically like this:
mRgba = inputFrame.rgba();
if (mask != null) {
Core.bitwise_and(mRgba, mask, mRgba);
}
if you had a Rect called sel, making the mask is not difficult either:
mask = Mat.zeros(mRgba.size(), mRgba.type()); // all black:
mask.submat(sel).setTo(Scalar.all(255)); // white square
now you need to make your Activity implement onTouchListener, and implement onTouch, 1st press should be tl corner of the rect, 2nd press br corner. all in all, we've got this:
package com.berak.cam3;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.app.Activity;
public class CamMaskActivity extends Activity implements CvCameraViewListener2, OnTouchListener {
CameraBridgeViewBase mOpenCvCameraView;
Rect sel = new Rect();
Mat mRgba, mask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cam3);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.cam3_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.setOnTouchListener(this);
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
@Override
public void onResume() {;
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5,this, mLoaderCallback);
}
@Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
if (mask != null) {
Core.bitwise_and(mRgba, mask, mRgba);
}
return mRgba;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
// TODO Auto-generated method stub
}
@Override
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;
if (x<0||y<0||x>=cols||y>=cols) return false;
if ((sel.x==0 && sel.y==0) || (sel.width!=0 && sel.height!=0))
{
mask = null;
sel.x=x; sel.y=y;
sel.width = sel.height = 0;
} else {
sel.width = x - sel.x;
sel.height = y - sel.y;
if ( sel.width <= 0 || sel.height <= 0 ) { // invalid, clear it all
sel.x=sel.y=sel.width=sel.height = 0;
mask = null;
return false;
}
mask = Mat.zeros(mRgba.size(), mRgba.type());
mask.submat(sel).setTo(Scalar.all(255));
}
Log.w("touch",sel.toString());
return false;
}
}