什么是 "twice HSV transformation"?

What is a "twice HSV transformation"?

这个方法是跟SPIE Proceeding article学的,他们用两次HSV变换来检测阴影。在他们的论文中,该方法描述如下:

Firstly, the color model of the image is transformed from RGB to HSV, and the three components of the HSV model are normalized to 0 to 255, then the image is transformed from RGB to HSV once again. Thirdly, the image is turned into a gray image from a color image, only the gray value of the red component is used. Fourthly, the OTSU thresholding method is used to produce a threshold by which the image is converted to a binary image. Since the gray value of the shadow area is usually smaller than those areas which are not covered by shadow, the objective is pixels whose gray value is below the threshold, and background is pixels whose gray value is beyond the threshold.

第二步和第三步有意义吗?

第二个和第三个陈述绝对没有任何意义。甚至管道也很可疑。然而,在重新阅读该声明可能十几次之后,这就是我想出的。对理解中的任何错误表示歉意。

我们先说第二点:

Firstly, the color model of the image is transformed from RGB to HSV, and the three components of the HSV model are normalized to 0 to 255, then the image is transformed from RGB to HSV once again

您很清楚,将图像从 RGB 转换为 HSV 会产生另一个三通道输出。根据您使用的平台,您将获得第一个通道或色调的 0-360 或 0-1,第二个通道或饱和度的 0-100 或 0-255,以及 0-100 或 0-255对于第三个渠道或价值。与其他通道相比,每个通道的幅度可能不相等,因此这些通道被独立地归一化到 0-255 范围。具体来说,这意味着色相、饱和度和明度分量都被归一化,因此它们的范围都在 0-255 之间。

完成此操作后,我们现在有了一个 HSV 图像,其中每个通道的范围为 0-255。我的猜测是他们将此 new 图像称为 RGB 图像,因为通道都在 0-255 范围内,就像任何 8 位 RGB 图像一样.这也是有道理的,因为当您将图像从 RGB 转换为 HSV 时,通道的动态范围都在 0-255 之间,所以我的猜测是它们将第一个 HSV 结果中的所有通道归一化以使其适合下一步。

按照上述方法在进行 HSV 转换后对通道进行归一化后,他们对这个新结果进行另一个 HSV 转换。他们为什么会在 second 时间内这样做的原因超出了我的范围并且没有任何意义,但这就是我从上面的描述中收集到的,这就是他们可能的意思 "twice HSV transformation" - 要将原始 RGB 图像一次转换为 HSV,将结果归一化,使所有通道跨越 0-255,然后再次重新应用 HSV 转换为这个中间结果。

我们来看第三点:

Thirdly, the image is turned into a gray image from a color image, only the gray value of the red component is used.

第二次变换HSV图像后的输出,最后的结果只是取第一个通道,本质上是灰度图像,是"red"通道。巧合的是,这也对应了你进行 HSV 转换后的 Hue。我不太确定在使用 HSV 两次转换图像后 Hue 通道具有什么属性,但也许它适用于这种特定方法。


我决定试一试,看看是否真的有效。这是我在网上找到的阴影示例图片:

来源:http://petapixel.com/

基本流程是获取图像,将其转换为 HSV,重新规范化图像,使值再次为 0-255,再进行一次 HSV 转换,然后通过 Otsu 进行自适应阈值处理。我们将阈值设定在最佳值以下以分割出阴影。

我打算使用 OpenCV Python,因为我的计算机上没有设置 C++ 库。在 OpenCV 中,当将图像转换为 HSV 时,如果图像是无符号的 8 位 RGB,Saturation 和 Value 分量会自动缩放到 [0-255],但 Hue 分量会缩放到 [0-179],以便将色调(最初是 [0-360))适合数据类型。因此,我将每个值按 (255/179) 缩放,以便将色调标准化为 [0-255]。这是我写的代码:

import numpy as np # Import relevant libraries
import cv2

# Read in image
img = cv2.imread('shadow.jpg')

# Convert to HSV
hsv1 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# Renormalize Hue channel to 0-255
hsv1[:,:,0] = ((255.0/179.0)*hsv1[:,:,0]).astype('uint8')

# Convert to HSV again
# Remember, channels are now RGB
hsv2 = cv2.cvtColor(hsv1, cv2.COLOR_RGB2HSV)

# Extract out the "red" channel
red = hsv2[:,:,0]

# Perform Otsu thresholding and INVERT the image
# Anything smaller than threshold is white, anything greater is black
_,out = cv2.threshold(red, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# Show the image - shadow mask
cv2.imshow('Output', out)
cv2.waitKey(0)
cv2.destroyAllWindows()

这是我得到的输出:

嗯....很明显有一些嘈杂的像素,但我想它确实有效......有点!