aburx
aburx

Reputation: 29

Change color of h value

I set my mask from BGR2HSV. I have my image:

mask.png

How I can change the white color in the mask? So I want to change the white parts with other colors.

Mat mask;
mask = imread("C:\\Users\\...\\Desktop\\...\\mask.png");
if (!img.data)
{
    cout << "Could not find the image";
    return -1;
}

cvtColor(mask, mask, COLOR_BGR2HSV);



cvtColor(mask, mask, COLOR_HSV2BGR);


imshow("Ergebnis", mask);
waitKey(0);

Upvotes: 2

Views: 906

Answers (1)

Burak
Burak

Reputation: 2495

Between two cvtColor functions, you need to split the image into its 3 channels with split. Looking at the conversion between RGB and HSV, make S channel 0 and choose an H value between [0-180]. Then, merge the channels back.

cv::Mat hsv = mask.clone(); // from your code
std::vector<cv::Mat> hsv_vec;
cv::split(hsv, hsv_vec);
cv::Mat &H = hsv_vec[0];
cv::Mat &S = hsv_vec[1];
cv::Mat &V = hsv_vec[2];
S = 0;
mask = (V > 10); // non-zero pixels in the original image
H(mask) = your_H_value_here; // H is between 0-180 in OpenCV
cv::merge(hsv_vec, hsv);
mask = hsv; // according to your code

As a side note, I suggest using convenient names for variables.

Upvotes: 2

Related Questions