Mark color of skin
Previously, we extracted the effective area of red on the image by marking the red on channel H. Similarly, we can limit the range of skin color and extract faces to achieve the effect of matting.
First of all, skin color should not only pay attention to the H channel, but also need to pay attention to the S channel. So, we first need to introduce a function: split(), defined as follows:
img = cv2.imread("4.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
Copy the code
As shown in the code above, we can use the cv2.splite() function to get the values of all the channels on the HSV image.
First, we assume a rough range of skin tones between [5,170] and saturation between [25,166]. In this way, we can use the previous section to get the image in the limited range as follows:
import cv2
img = cv2.imread("4.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
hmask=cv2.inRange(h,5.170)
smask=cv2.inRange(s,25.166)
mask=hmask & smask
result=cv2.bitwise_and(img,img,mask=mask)
cv2.imshow("img",img)
cv2.imshow("result", result)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
After running, as long as the skin color of the image, will be extracted, and the rest of the black.
Achieve artistic effect
Previously, we have introduced the application of various HS channels without singly emphasizing or emphasizing the importance of V. Here, let’s do some interesting art with v-channel values.
import cv2
img = cv2.imread("9.jpg")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
v[:,:]=255
newHSV=cv2.merge([h,s,v])
result=cv2.cvtColor(newHSV,cv2.COLOR_HSV2BGR)
cv2.imshow("img",img)
cv2.imshow("result", result)
cv2.waitKey()
cv2.destroyAllWindows()
Copy the code
After it runs, it feels a little bit out of the mainstream.