gpt4 book ai didi

python - 如何使用OPENCV从原始图像中减去图像的 “hue”

转载 作者:行者123 更新时间:2023-12-02 17:37:43 27 4
gpt4 key购买 nike

我正在尝试从该图像中减去图像的“色相”部分。我已经提取了所有的h,s,v分量。但是我不知道下一步该怎么办?有可能吗?

这是我的代码

import cv2

def showimage(text,img):
cv2.imshow(text,cv2.resize(img, (700, 700)))
cv2.waitKey(0)
return 0


# Read image in BGR
img_path = "new.jpg"
img = cv2.imread(img_path)
showimage("orig",img)
# Convert BGR to HSV and parse HSV
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
showimage("hsv",hsv_img)
h, s, v = hsv_img[:, :, 0], hsv_img[:, :, 1], hsv_img[:, :, 2]
showimage("h",h)
showimage("s",s)
showimage("v",v)

sub=hsv_img-h
cv2.destroyAllWindows()

最佳答案

您不能减去hsv_imgh,因为它们的尺寸不相同。但是,您可以使用h减去图像的灰度版本。为此,添加以下行-

gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sub = gray_img-h
showimage("sub",sub)

但是,如果您尝试使用零色相成分来可视化图像,则可以使用以下代码进行操作-

例:

enter image description here enter image description here
import cv2
import numpy as np

def showimage(text,img):
cv2.imshow(text,cv2.resize(img, (500,500)))
cv2.waitKey(0)
return 0


# Read image in BGR
img_path = "new.jpg"
img = cv2.imread(img_path)
showimage("orig",img)
# Convert BGR to HSV and parse HSV
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
showimage("hsv",hsv_img)
h, s, v = hsv_img[:, :, 0], hsv_img[:, :, 1], hsv_img[:, :, 2]
showimage("h",h)
showimage("s",s)
showimage("v",v)

h = np.zeros_like(h)

img2 = cv2.merge((h,s,v))
img2 = cv2.cvtColor(img2, cv2.COLOR_HSV2BGR)
showimage("With h=0 ", img2)

cv2.destroyAllWindows()

关于python - 如何使用OPENCV从原始图像中减去图像的 “hue”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48661548/

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