Ask Your Question
2

subtract color from Mat

asked 2014-04-11 04:18:42 -0600

ntb-werz gravatar image

updated 2015-10-20 08:53:31 -0600

Hello everybody

I'm new in OpenCV and I have a Question:

I have an Image like that: original image 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 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 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 retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
7

answered 2014-04-11 05:31:33 -0600

berak gravatar image

first of all, transparency is not achieved by subtraction, but by setting values in the alpha channel.

//Mat mRgb = imread("sqr.png");

// first, split it into seperate channels:
Mat channels[4];
split(mRgb,channels);

// then calculate a mask, that will serve as an alpha channel:
Scalar min_color(0,0,0);
Scalar max_color(255,255,0);
// i know, the above looks counter-intuitive, 
// but the alpha chan will actually contain 'opacity' values, (so 0 is transparent)
inRange(mRgb, min_color,  max_color, channels[3]);

// finally merge them into a 4channel img with transparency: 
Mat rgba;
cv::merge(channels,4,rgba);

//imwrite("myrgba.png",rgba);
edit flag offensive delete link more

Comments

You posted first, shall I delete my answer....:)

Haris gravatar imageHaris ( 2014-04-11 05:49:55 -0600 )edit

you got the nicer pictures, keep it . ;)

berak gravatar imageberak ( 2014-04-11 06:23:12 -0600 )edit

thank you really much for your help, I try to implement your solution. thank you

ntb-werz gravatar imagentb-werz ( 2014-04-11 07:06:36 -0600 )edit
8

answered 2014-04-11 05:43:28 -0600

Haris gravatar image
  1. Use inRange() to create alpha.
  2. Invert alpha.
  3. Split source.
  4. Merge source channel to final image including alpha.

      //Laod source and create alpha   
      Mat src=imread("red.png",1);   
      Mat alpha;   
      inRange(src, Scalar(0,0,250),  Scalar(0,0,255),alpha);    
      bitwise_not(alpha,alpha);
    
      //split source  
      Mat bgr[3];   
      split(src,bgr);   
    
      //Merge to final image including alpha   
      Mat tmp[4] = { bgr[0],bgr[1],bgr[2],alpha};   
      Mat dst;   
      merge(tmp,4,dst);   
      imwrite("dst.png",dst);
    

image description

image description

edit flag offensive delete link more

Comments

thank you really much for your help, I try to implement your solution. thank you

ntb-werz gravatar imagentb-werz ( 2014-04-11 07:06:48 -0600 )edit

Question Tools

Stats

Asked: 2014-04-11 04:18:42 -0600

Seen: 11,654 times

Last updated: Aug 24 '15