尝试将 HSV 图像转换为黑白图像 [opencv]

Trying to convert HSV image to Black and white [opencv]

我有一个皮肤检测代码,可以很好地从 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

但它会产生这个输出 -

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

你基本上是在寻找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.