简体   繁体   English

如何使用 OpenCV-Python 更改某个区域的色调

[英]How can I change the hue of a certain area with OpenCV-Python

i want to edit the hsv value of a certain area The picture I want to change color of我想编辑某个区域的hsv值我想改变颜色的图片

I to change all the purple parts of the image to green.我将图像的所有紫色部分更改为绿色。 Here is the result I made in an image editing software just to show a example.这是我在图像编辑软件中制作的结果,只是为了展示一个例子。 The Result Yeah i made it darker for no reason but i just wanna make the blade green but not the handle结果是的,我无缘无故地让它变黑了,但我只想让刀片变绿而不是手柄

The simple way in Python/OpenCV is to create a mask where the image is purple using inRange() and then change the image to green where the mask is white using Numpy. Python/OpenCV 中的简单方法是使用 inRange() 创建图像为紫色的蒙版,然后使用 Numpy 将蒙版为白色的图像更改为绿色。

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# load image with alpha channel
img = cv2.imread('sword_purple.png', cv2.IMREAD_UNCHANGED)

# extract alpha channel
alpha = img[:,:,3]

# extract bgr channels
bgr = img[:,:,0:3]

# select purple
lower_purple = (140,40,110)
upper_purple = (170,60,130)
mask = cv2.inRange(bgr, lower_purple, upper_purple)

# change the image to make it green where the mask is white
bgr_new = bgr.copy()
bgr_new[mask==255] = (0,255,0)

# put alpha back into rgb_new
bgra = cv2.cvtColor(bgr_new, cv2.COLOR_BGR2BGRA)
bgra[:,:,3] = alpha

# save output
cv2.imwrite('sword_alpha.png', alpha)
cv2.imwrite('sword_bgr.png', bgr)
cv2.imwrite('sword_mask.png', mask)
cv2.imwrite('sword_masked_green.png', bgr_new)
cv2.imwrite('sword_green.png', bgra)

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

Alpha channel: 阿尔法通道:

在此处输入图像描述

BGR channels: BGR 渠道:

在此处输入图像描述

Mask:面具:

在此处输入图像描述

Recolored BGR channels:重新着色的 BGR 通道:

在此处输入图像描述

Result with alpha channel put back:放回 alpha 通道的结果:

在此处输入图像描述

Alternately, you can do the same by converting BGR to HSV and changing the purple to green the same way.或者,您可以通过将 BGR 转换为 HSV 并以相同的方式将紫色更改为绿色来执行相同的操作。 Then convert back to BGR and put the alpha channel back.然后转换回 BGR 并放回 alpha 通道。

Here is a way to do that in Python/OpenCV by shifting hues.这是一种在 Python/OpenCV 中通过改变色调来做到这一点的方法。

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# load image with alpha channel
img = cv2.imread('sword_purple.png', cv2.IMREAD_UNCHANGED)

# extract alpha channel
alpha = img[:,:,3]

# extract bgr channels
bgr = img[:,:,0:3]

# convert to HSV
hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
#h = hsv[:,:,0]
#s = hsv[:,:,1]
#v = hsv[:,:,2]
h,s,v = cv2.split(hsv)

# purple is 276 in range 0 to 360; so half in OpenCV
# green is 120 in range 0 to 360; so half in OpenCV
purple = 138
green = 60

# diff color (green - hue)
diff_color = green - purple

# modify hue channel by adding difference and modulo 180
hnew = np.mod(h + diff_color, 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)

# put alpha back into bgr_new
bgra = cv2.cvtColor(bgr_new, cv2.COLOR_BGR2BGRA)
bgra[:,:,3] = alpha

# save output
cv2.imwrite('sword_alpha.png', alpha)
cv2.imwrite('sword_bgr.png', bgr)
cv2.imwrite('sword_bgr_new.png', bgr_new)
cv2.imwrite('sword_green.png', bgra)

# Display various images to see the steps
cv2.imshow('alpha',alpha)
cv2.imshow('bgr',bgr)
cv2.imshow('bgr_new',bgr_new)
cv2.imshow('bgra',bgra)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result: 结果:

在此处输入图像描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM