0

I have an image stored in RGB color space and I need to detect yellow pixel and increment each one by 5. For example, if I have a photo with a yellow lemon and a brown table, I have to turn the lemon more yellow and the table must remain the same. Then I have to save the new image.

How can I perform it with openCV and C++?

Davide577
  • 31
  • 5
  • 1
    This should help https://stackoverflow.com/a/52183666/2836621 – Mark Setchell Feb 11 '19 at 14:11
  • Another example to find color in HSV: [how-to-define-a-threshold-value-to-detect-only-green-colour-objects-in-an-image](https://stackoverflow.com/questions/47483951/how-to-define-a-threshold-value-to-detect-only-green-colour-objects-in-an-image/47483966#47483966) – Kinght 金 Feb 12 '19 at 09:52

1 Answers1

3

Yes.

  1. Convert image into HSV color space.

  2. Calculate yellow range in HSV (from Scalar to Scalar).

  3. Create binary mask for yellow: inRange.

  4. Call add with mask from (3) for your HSV image and cv::Scalar(5, 0, 0)

  5. Convert Result to RGB.

Example:

cv::Mat rgbImg = cv::imread("src.jpg", cv::IMREAD_COLOR);
cv::Mat hsvImg;
cv::cvtColor(rgbImg, hsvImg, cv::COLOR_BGR2HSV);
cv::Mat threshImg;
cv::inRange(hsvImg, cv::Scalar(20, 100, 100), cv::Scalar(30, 255, 255), threshImg);
cv::imwrite("thresh.png", threshImg);
cv::add(hsvImg, cv::Scalar(5, 0, 0), hsvImg, threshImg);
cv::cvtColor(hsvImg, rgbImg, cv::COLOR_HSV2BGR);
cv::imwrite("res.png", rgbImg);

And pictures: Source image: Binary mask: Result:

Nuzhny
  • 1,869
  • 1
  • 7
  • 13