Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ | Change color in cv::mat with setTo

Tags:

c++

opencv

I have a cv::Mat file with vec3b values. These values are colours from an image. I'd like to change some colours in that image.

I know the setTo() function for normal matrix manipulation, but how do I use it for my Mat file?

I tried something like this:

 image = image.setto(Vec3b(0,0,0), image == Vec3b(255,0,0))

Thx!

like image 565
Anton Mulder Avatar asked Mar 22 '23 02:03

Anton Mulder


1 Answers

Given an image image, we want to find all the pixels in image that are equal to Scalar(255,0,0) and then set these pixels to Scalar(0,0,0).

  • First we need to obtain mask, such that a location in mask is set to 255 if the corresponding location in image equals Scalar(255,0,0), otherwise it is set to 0. This can be achieved with inRange() function.

    Mat mask;
    inRange(image, Scalar(255,0,0), Scalar(255,0,0), mask);
    
  • Now apply setTo() function to image.

    image.setTo(Scalar(0,0,0), mask);
    
like image 165
Alexey Avatar answered Mar 27 '23 23:03

Alexey