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

时间:2015-03-29 09:21:25

标签: python opencv pixel hsv

我有一个皮肤检测代码可以很好地将皮肤像素与 HSV 图像的背景分开。

enter image description here

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

基本上,像素将是[0-255,0,255]。我在opencv中使用python。

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

但它产生了这个输出 -

enter image description here

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

1 个答案:

答案 0 :(得分: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.