Ron Gross
Ron Gross

Reputation: 1624

OpenCV converting one color to another

I have a greyscale image I would like to convert all the white pixels (pure white - 255) to black (0) only those colors, not all the greyscale How can I do this? Thank you! Ron

Upvotes: 2

Views: 9332

Answers (2)

Alexey
Alexey

Reputation: 5978

You can do the following.

  1. Threshold your image using inRange().

    Mat image;  // your original image
    Mat mask;
    void inRange(image, Scalar(255), Scalar(255), mask)
    

    The output mask will be a binary mask where its pixel is set to 255 if the corresponding pixel in image equals to 255 (that is if the pixel value is between 255 and 255, boundaries are inclusive).

  2. Copy black_image (your desired substitute color) into your original image through mask using copyTo().

    Mat black_image(image.size(), CV_8U, Scalar(0));  // all zeros image
    black_image.copyTo(image, mask);
    

Upvotes: 6

Andrey  Smorodov
Andrey Smorodov

Reputation: 10852

Use cvThreshold, see the last case "Threshold to Zero, Inverted" here: http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html

Set threshold value to 254, I think this will solve your problem.

Upvotes: 1

Related Questions