使用OpenCV更改像素的颜色

时间:2015-03-18 14:56:00

标签: python opencv pixel

假设我有这种玫瑰(不关心背景,只有白叶很重要)。

enter image description here

我将其转换为灰度图片: grayscaled=cv2.imread('white_rose.png',cv2.IMREAD_GRAYSCALE)

如果红色(R=255)与白色像素具有相同的对比度,我怎样才能将每个白色像素更改为红色像素?意思是我想看到白色的叶子是红色的,但是L中每个像素的grayscaled值都相同?

1 个答案:

答案 0 :(得分:1)

您需要在灰色图像上循环并自己创建一个新的彩色图像。

对于每个像素,您可以将彩色图像的R值替换为余下的255和相对灰度值:

import cv2
import numpy as np

img = cv2.imread('5585T.jpg')
print type(img)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
new=[[[0,0,255%j] for j in i] for i in img_gray]
dt = np.dtype('f8')
new=np.array(new,dtype=dt)

cv2.imwrite('img.jpg',new)

enter image description here

new=[[[255%j,255%j,j] for j in i] for i in img_gray]

enter image description here

相关问题