gpt4 book ai didi

python - 使用python更改图像中的颜色范围

转载 作者:行者123 更新时间:2023-12-02 18:34:10 25 4
gpt4 key购买 nike

我正在使用 python 自动更改图像颜色。我正在使用的图像如下,我想将它从红色移动到其他颜色范围,比如绿色,尽可能保留细节和阴影。我已经能够将图像的一些转换为纯色,但会丢失所有细节。

Blue instead of red.

我目前使用的代码如下,我不太清楚红色的正确范围以使其正常工作,而且它只能转换为单一颜色,再次丢失所有细节和阴影。

感谢任何帮助,谢谢。

import cv2
import numpy as np
import skimage.exposure

# load image and get dimensions
img = cv2.imread("test5.jpg")

# convert to hsv
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

## mask of upper red (170,50,50) ~ (180,255,255)
## mask of lower red (0,50,50) ~ (10,255,255)
# threshold using inRange
range1 = (0,50,50)
range2 = (1,255,255)
mask = cv2.inRange(hsv,range1,range2)
mask = 255 - mask

# apply morphology opening to mask
kernel = np.ones((3,3), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

# antialias mask
mask = cv2.GaussianBlur(mask, (0,0), sigmaX=3, sigmaY=3, borderType = cv2.BORDER_DEFAULT)
mask = skimage.exposure.rescale_intensity(mask, in_range=(127.5,255), out_range=(0,255))

result = img.copy()
result[mask==0] = (255,255,255)

# write result to disk
cv2.imwrite("test6.jpg", result)

最佳答案

这是解决 Python/OpenCV 问题的一种方法。但是对于红色,很难做到,因为红色跨越 0 色调,这也是图像中灰色、白色和黑色的色调。您遇到的另一个问题是肤色有红色阴影,因此您不能为颜色选择太大的范围。此外,在处理红色范围时,您需要两组,一组用于最高 180 的色调,另一组用于高于 0 的色调。

输入:

enter image description here

import cv2
import numpy as np

# load image
img = cv2.imread('red_clothes.jpg')

# convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)

blue_hue = 120
red_hue = 0

# diff hue (blue_hue - red_hue)
diff_hue = blue_hue - red_hue

# create mask for red color in hsv
lower1 = (150,150,150)
upper1 = (180,255,255)
mask1 = cv2.inRange(hsv, lower1, upper1)
lower2 = (0,150,150)
upper2 = (30,255,255)
mask2 = cv2.inRange(hsv, lower2, upper2)
mask = cv2.add(mask1,mask2)
mask = cv2.merge([mask,mask,mask])

# apply morphology to clean mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,9))
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

# modify hue channel by adding difference and modulo 180
hnew = np.mod(h + diff_hue, 180).astype(np.uint8)

# recombine channels
hsv_new = cv2.merge([hnew,s,v])

# convert back to bgr
bgr_new = cv2.cvtColor(hsv_new, cv2.COLOR_HSV2BGR)

# blend with original using mask
result = np.where(mask==(255, 255, 255), bgr_new, img)

# save output
cv2.imwrite('red_clothes_mask.png', mask)
cv2.imwrite('red_clothes_hue_shift.png', bgr_new)
cv2.imwrite('red_clothes_red2blue.png', result)

# Display various images to see the steps
cv2.imshow('mask1',mask1)
cv2.imshow('mask2',mask2)
cv2.imshow('mask',mask)
cv2.imshow('bgr_new',bgr_new)
cv2.imshow('result',result)
cv2.waitKey(0)
cv2.destroyAllWindows()

面具:

enter image description here

色调偏移图像:

enter image description here

使用蒙版混合输入和色相偏移图像之间的混合:

enter image description here

所以结果是有 Blob 的,因为黑色和红色混合在一起,而且由于肤色的原因,范围有限。

关于python - 使用python更改图像中的颜色范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69023789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com