智一面的面试题提供python的测试题
说明一下去除背景,或者提取人物特征的算法,
import cv2
class skin_judge():
def __init__(self,img):
self.img = img
def read_img(self):
self.Raw_img = cv2.imread(self.img)
return self.Raw_img
def skin_judge(self):
width,length=self.Raw_img.shape[:2]
print(width,length)
for i in range (width):
for j in range(length):
b,g,r = self.Raw_img[i,j]##读取各个像素点的BGR信息
##将BGR转为YCbCr
Y = 0.257*r + 0.564*g + 0.098*b + 16
Cb = -0.148*r - 0.291*g + 0.439*b +128
Cr = 0.439*r - 0.368*g -0.071*b + 128
if 133<=Cr and Cr<=173 and 77<=Cb and Cb<=127:###黄种人的皮肤颜色范围
self.Raw_img[i,j]=[255,255,255]
else:
self.Raw_img[i,j]=[0,0,0]
return self.Raw_img ###返回处理过的图片
Img = skin_judge('skin_judge_2.jpg')
cv2.imshow('Raw.jpg',Img.read_img())
cv2.imshow('judged.jpg',Img.skin_judge())
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
class skin_judge():
def __init__(self,img):
self.img = img
def read_img(self):
self.Raw_img = cv2.imread(self.img)
return self.Raw_img
def skin_judge(self):
width,length=self.Raw_img.shape[:2]
print(width,length)
for i in range (width):
for j in range(length):
b,g,r = self.Raw_img[i,j]##读取各个像素点的BGR信息
##将BGR转为YCbCr
Y = 0.257*r + 0.564*g + 0.098*b + 16
Cb = -0.148*r - 0.291*g + 0.439*b +128
Cr = 0.439*r - 0.368*g -0.071*b + 128
if 133<=Cr and Cr<=173 and 77<=Cb and Cb<=127:###黄种人的皮肤颜色范围
self.Raw_img[i,j]=[255,255,255]
else:
self.Raw_img[i,j]=[0,0,0]
return self.Raw_img ###返回处理过的图片
Img = skin_judge('skin_judge_2.jpg')
cv2.imshow('Raw.jpg',Img.read_img())
cv2.imshow('judged.jpg',Img.skin_judge())
cv2.waitKey()
cv2.destroyAllWindows()