使用 OpenCV 更改像素的颜色 [英] Change color of a pixel with OpenCV

查看:84
本文介绍了使用 OpenCV 更改像素的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这朵玫瑰(不要在意背景,只有白叶很重要).

Let say I have this rose (Do not care about the background, only the white leaves are important).

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

I transform it to a grayscale picture: grayscaled=cv2.imread('white_rose.png',cv2.IMREAD_GRAYSCALE)

如何在红色 (R=255) 与白色像素具有相同对比度的情况下将每个白色像素更改为红色像素?这意味着我想看到红色的白色叶子,但每个像素的 L 值与 grayscaled 中的值相同?

How can I change every white pixel to a red one under the condition the red color (R=255) will have the same contrast as the white one has ? Meaning I want to see the white leaves in red color but with the same L value of every pixel that in grayscaled ?

推荐答案

您需要遍历您的灰色图像并自己创建一个新的彩色图像.

You need to loop over your grey image and create a new coloured image by yourself.

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

For each pixel, you can replace the R value of your coloured image with the remainder of dividing of 255 and relative grey value:

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)

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

这篇关于使用 OpenCV 更改像素的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆