toto_tata
toto_tata

Reputation: 15392

OpenCV: how to change color of all non transparent pixels to black?

I use OpenCV (Java) and I want to change color of all the non transparent pixels in my Mat to black (colors defined in ARGB_8888 but could be another format)

How can I do that ?

  Mat mat = new Mat();
  bmp32 = bitmap.copy(Bitmap.Config.ARGB_8888, true);
  Utils.bitmapToMat(bmp32, mat);
// and then ????

Thanks !

Upvotes: 0

Views: 341

Answers (1)

Burak
Burak

Reputation: 2495

// allocate space as mixChannels requires so
Mat bgr = new Mat(mat.size(), CvType.CV_8UC3);
Mat alpha = new Mat(mat.size(), CvType.CV_8U);
List<Mat> src = new ArrayList<Mat>();
src.add(mat);
List<Mat> dst = new ArrayList<Mat>();
dst.add(alpha);
dst.add(bgr);
// A->alpha, RGB->bgr in reverse order
MatOfInt fromTo = new MatOfInt(0,0, 1,3, 2,2, 3,1);
mixChannels(src, dst, fromTo);

// now, make non-transparent parts black
Mat mask;
compare​(alpha, Scalar(0), mask, opencv_core.CMP_EQ);
Mat res;
Core.bitwise_and(bgr, bgr, res, mask);

Upvotes: 1

Related Questions