Ask Your Question
0

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

asked 2019-08-14 14:47:53 -0500

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.

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2019-08-14 18:20:52 -0500

see the lines from OpenCV Samples imageSegmentation.cpp as a sample what you already have done.

and here is a better alternative

    Mat mask;
    inRange(src, Scalar(255, 255, 255), Scalar(255, 255, 255), mask);
    src.setTo(Scalar(0, 0, 0), mask);
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-08-14 14:47:53 -0500

Seen: 12 times

Last updated: Aug 14