【问题标题】:Trying to convert HSV image to Black and white [opencv] [closed]试图将 HSV 图像转换为黑白 [opencv] [关闭]
【发布时间】:2015-03-29 09:21:25
【问题描述】:

我有一个皮肤检测代码,可以很好地从 HSV 图像的背景中分离出皮肤像素。

现在,我想将右侧的图像转换为黑白图像。 我的逻辑是检查所有非黑色像素并将其饱和度值更改为 0,强度值更改为 255。

所以本质上,像素将是 [0-255,0,255]。我正在使用 python 和 opencv。

h,b = skin.shape[:2]    

    for i in xrange(h):
        for j in xrange(b):
            # check if it is a non-black pixel
            if skin[i][j][2]>0:
                # set non-black pixel intensity to full
                skin[i][j][2]=255
                # set saturation zero
                skin[i][j][1]=0

但它会产生这个输出 -

如何将那些粉红色像素转换为白色??

【问题讨论】:

  • 你引用了skin[i][j][2](值)两次——你永远不会改变色相或饱和度。您的代码可能会更清晰,例如HUE, SAT, VAL = range(3),那就更难犯那样的错误了。
  • 我不知道opencv,但你不应该用skin[i][j][1]=0 将饱和度设置为0吗?
  • @PM2Ring 是的,谢谢。我编辑了那一点。我在发布之前已经更改了一些代码,但忘记重新检查了。
  • 查看发布的答案?

标签: python opencv pixel hsv


【解决方案1】:

你基本上是在寻找Thresholding,基本上这个概念是选择一个阈值,任何大于阈值的像素值(灰度)被设置为白色和黑色。 OpenCV 有一些漂亮的内置方法可以做同样的事情,但它是非常简单的代码:

skin = #Initialize this variable with the image produced after separating the skin pixels from the image.

bw_image = cv2.cvtColor(skin, cv2.HSV2GRAY)

new_image = bw_image[:]

threshold = 1 
 #This value is set to 1 because We want to separate out the pixel values which are purely BLACK whose grayscale value is constant (0) 

现在我们简单地迭代图像并相应地替换值。

h,b = skin.shape[:2]    

for i in xrange(h):
    for j in xrange(b):
        if bw_image[i][j] > threshold:
            new_image[i][j] = 255 #Setting the skin tone to be White
        else:
            new_image[i][j] = 0 #else setting it to zero.

【讨论】:

  • 谢谢!虽然,在我的 opencv 版本(2.4.5)中找不到 cv2.HSV2GRAY。于是我改成了BGR,然后又改成了灰度。
猜你喜欢
  • 2011-11-29
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
相关资源
最近更新 更多