Ask Your Question

Revision history [back]

change all instances of a colour to a different one in C++

I would like to take all pure white pixels (255, 255, 255) from a CV_8UC3 image, and convert them to something else, say grey (128, 128, 128).

I could iterate through all the pixels, inspect each one to get the colour, etc. mat.at<uint8_t>(point) but that's not efficient.

What I did was create a new image with the colour I wanted, create a mask of the areas I want from the original image, and copy the masked area overtop of the new background colour. Looks a bit like this:

cv::Mat mask = cv::Mat::zeros(fg.size(), CV_8UC1);
mask = (mat == 0); // example: only mask the pure black pixels from the original image
cv::Mat newImage(mat.size(), CV_8UC3, cv::Scalar(128, 128, 128)); // create the new image
mat.copyTo(newImage, mask);

While this works, it is backwards and I suspect not as efficient as masking the white pixels and changing just those. I think the correct way would have been to mask the white area only, and then set that masked area to the new colour.

cv::Mat mask = cv::Mat::zeros(fg.size(), CV_8UC1);
mask = (mat == 255); // create a mask of only the white pixels
// ...but then what?

The problem is I'm not sure how to efficiently changed those masked pixels to the new colour.