Thresholding an HSV image

asked 2015-03-29 03:21:33 -0600

updated 2015-03-29 04:10:07 -0600

berak gravatar image

Hello, I have a skin detection code which get the skin region from an HSV image. I want to threshold it to a binary image. Here's a sample output-

image description

I want the image on the right to be a binary image. The following snippet returns an error-

frame = cv2.resize(frame,(100,100))
        converted = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        skinMask = cv2.inRange(converted, l1, l2)
        # using an elliptical kernel
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,5))
        skinMask = cv2.erode(skinMask, kernel, iterations = 2)
        skinMask = cv2.dilate(skinMask, kernel, iterations = 2)

        # blur the mask to help remove noise, then apply the
        # mask to the frame
        skinMask = cv2.medianBlur(skinMask, 3)
        skin = cv2.bitwise_and(frame, frame, mask = skinMask)

            #This is my addition that causes error
        skin = cv2.cvtColor(skin,cv2.COLOR_HSV2BGR)
        skin = cv2.threshold(skin,10,255,cv2.THRESH_BINARY )
        skin = cv2.cvtColor(skin,cv2.COLOR_BGR2HSV)
        # # show the skin in the image along with the mask
        cv2.imshow("images", np.hstack([frame,skin]))

How do I get a binary version of skin image?

edit retag flag offensive close merge delete


" The following snippet returns an error-" And the 1million $-question is: "WHICH ERROR?"

FooBar gravatar imageFooBar ( 2015-03-29 07:27:30 -0600 )edit

you already got the 'skinMask'. use that !

berak gravatar imageberak ( 2015-03-29 07:34:01 -0600 )edit