如何使用OPENCV从原始图像中减去图像的“色调”

时间:2018-02-07 10:32:47

标签: python opencv computer-vision

我试图从该图像中减去图像的“色调”部分。我已经提取了所有的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()

1 个答案:

答案 0 :(得分:0)

您无法减去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()