简体   繁体   English

使用 python opencv 将二进制图像中的黑色像素转换为红色

[英]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?我有一个黑白二值图像,我使用 opencv 创建我想将此图像上的黑色像素转换为红色,我该如何实现?

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您可以在 Python/OpenCV 中通过将 blackAndWhiteImage 转换为 3 个通道来执行此操作,然后使用 Numpy 使用单通道 blackAndWhiteImage 作为掩码来更改颜色

blackAndWhiteImage3 = cv2.cvtColor(blackAndWhiteImage, cv2.COLOR_GRAY2BGR)
blackAndWhiteImage3[blackAndWhiteImage==0] = (0,0,255)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python计算二进制图像中的黑白像素 - how to count black and white pixels in binary image using python 在OpenCV python中将白色像素转换为黑色 - Convert White Pixels to Black in OpenCV python 使用 OpenCV 在 Python 中计算图像中的黑色像素数 - count number of black pixels in an image in Python with OpenCV 在Python中使用OpenCV将图像与另一个包含黑色像素的非矩形图像重叠 - Overlaying an image with another non-rectangular image containing black pixels using OpenCV in Python Python OpenCV - 在二进制图像中查找黑色区域 - Python OpenCV - Find black areas in a binary image 使用opencv python将图像转换为黑白蒙版 - Convert an image to black and white mask using opencv python 使用OpenCV Python,如何使所有黑色像素透明,然后将其覆盖在原始图像上 - Using OpenCV Python, How would you make all black pixels transparent, and then overlay it over original image 数不了。 使用OpenCV的图像中的黑色到白色像素 - Counting the no. of black to white pixels in the image using OpenCV 将图像中的所有白色像素转换为黑色像素 - Convert all white pixels in the image into black pixels 如何才能在PIL图像中获得红色像素和黑色像素 - how can i get no of red pixels and no of black pixels in PIL image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM