Hello everybody
I'm new in OpenCV and I have a Question:
I have an Image like that:
original image
and i want to remove the red color and set this color to transparent. The result should look like this:
image without red
till now, I have following code:
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_RemovePink(JNIEnv*, jobject, jlong addrRgba);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_RemovePink(JNIEnv*, jobject, jlong addrRgba){
Mat& mRgb = *(Mat*)addrRgba;
Mat redOnly;
Scalar min_color = CV_RGB(0,0,0);
Scalar max_color = CV_RGB(0,0,255);
inRange(mRgb, min_color, max_color, redOnly);
}
which should give my a mat like that: redOnly correct?
Now I want to substract the redOnly from the original image. how can I do that?
Thanks for every kind of help!
Edit
Thank you Haris and berak for your answers, you are pushing me to the answer,
But now I want to remove the red color from the CvCameraViewFrame from android device.
my code is following (Android Activity):
public class Tutorial2Activity extends Activity implements CvCameraViewListener2 {
private static final String TAG = "OCVSample::Activity";
private int mViewMode;
private Mat mRgba;
private CameraBridgeViewBase mOpenCvCameraView;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
System.loadLibrary("mixed_sample");
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.tutorial2_surface_view);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.tutorial2_activity_surface_view);
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
public void onPause(){
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume(){
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallback);
}
public void onDestroy(){
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
public void onCameraViewStarted(int width, int height) {
System.out.println("width="+width+" heigth="+height);
mRgba = new Mat(height, width, CvType.CV_8UC4);
}
public void onCameraViewStopped(){
mRgba.release();
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame){
mRgba = inputFrame.rgba();
RemovePink(mRgba.getNativeObjAddr());
return mRgba;
}
public boolean onOptionsItemSelected(MenuItem item){
return true;
}
public native void RemovePink( long matAddrRgba);
}
and the jni_part.cpp:
using namespace std;
using namespace cv;
extern "C" {
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_RemovePink(JNIEnv*, jobject, jlong addrRgba);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial2_Tutorial2Activity_RemovePink(JNIEnv*, jobject, jlong addrRgba){
Mat& mRgba = *(Mat*)addrRgba;
Mat alpha;
inRange(mRgba,Scalar(0,0,125), Scalar(20,20,255),alpha);
bitwise_not(alpha,alpha);
//split source
Mat bgr[4];
bgr[3];
split(mRgba,bgr);
//Merge to final image including alpha
Mat tmp[4] = { bgr[0],bgr[1],bgr[2],alpha};
merge(tmp,4,mRgba);
}
}
But on the camera preview I only get the "normal" picture, like it is without manipulation.
Has somebody an idea what I make wrong? Many thanks in advance!