Python替换下方或上方特定坐标的颜色

时间:2018-07-09 09:49:13

标签: python-2.7 opencv image-processing

场景

我有一张要替换一种颜色的图像(当然是在范围内)。这是一张床单图像,我想用其他颜色代替它,但我也不想失去反射效果。(我已经手动选择了范围值)

代码

import numpy as np
import cv2

image = cv2.imread('4.png')
cv2.imshow("images",image)

boundaries = [
    ([102,116,127], [203,218,227])
]
# loop over the boundaries
for (lower, upper) in boundaries:
    # create NumPy arrays from the boundaries
    lower = np.array(lower, dtype = "uint8")
    upper = np.array(upper, dtype = "uint8")

    # find the colors within the specified boundaries and apply
    # the mask
    mask = cv2.inRange(image, lower, upper)
    #print mask
    output = cv2.bitwise_and(image, image, mask = mask)


    # show the images
    cv2.imshow("images", np.hstack([image, output]))
    cv2.waitKey(0)

当前输出

enter image description here

我认为床是我面临的唯一问题(这就是为什么我只想替换床上方像素的颜色的原因),它的RGB值与床单相同(我猜)。我找不到任何可以获取此方法的资源。感谢您的指导,对此我是全新的。

更新

我再次尝试使用HSV值,并对结果感到满意,但是如何分配(更改)颜色?

After converting the image to HSV

0 个答案:

没有答案