0

I have a binary image in black and white, I created using opencv I would like to convert black pixels on this image to red, how can I achieve this?

def convertImageToBinary():
    print('converting image to black and white')
    originalImage = cv2.imread('lena.png')
    grayImage = cv2.cvtColor(originalImage, cv2.COLOR_BGR2GRAY)

    (thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 127, 255, cv2.THRESH_BINARY)


    cv2.imshow('Black white image', blackAndWhiteImage)
    cv2.imshow('Original image',originalImage)
    cv2.imshow('Gray image', grayImage)

    cv2.waitKey(0)
    cv2.destroyAllWindows()
0

1 Answer 1

4

You can do that in Python/OpenCV by converting your blackAndWhiteImage to 3 channels, then use Numpy to change the color using the single channel blackAndWhiteImage as a mask

blackAndWhiteImage3 = cv2.cvtColor(blackAndWhiteImage, cv2.COLOR_GRAY2BGR)
blackAndWhiteImage3[blackAndWhiteImage==0] = (0,0,255)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.