简体   繁体   中英

Convert black pixels with red in a binary image using python opencv

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()

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM