Je-Ar Zamora
Je-Ar Zamora

Reputation: 68

Changing the color of a mask OpenCV 3

I want to change the color of my mask which is black to any different color.

Here is my mask

enter image description here

and here is my original image

enter image description here

I know that if I use this function

src.copyTo(dst, mask);

it will allow me to use the mask properly and to have a result like this

enter image description here

However whenever I change the mask color to non black it doesn't make anything to dst Mat.

Is there a way to use a mask with non-black mask?

I don't want to use looping function because I know it'll cost me a lot of processing time.

Thanks

Upvotes: 0

Views: 2971

Answers (2)

Michał Gacka
Michał Gacka

Reputation: 3071

What you're looking for is a bitwise_and operation performed on the mask and the original image. If I understand you correctly, there is no reason for changing the color of the mask.

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150745

I don't write Java, but you can try in C++:

// assume that origin image has 3 channels
cv::Scalar myColor(255,0,255);  

// create dst with background color of your choice
cv::Mat dst(src.size(),src.type(),myColor);

// now copy
src.copyTo(dst, mask);

And the result:

enter image description here

Upvotes: 2

Related Questions