<jupyter_start><jupyter_text>To change the background of ID Photo<jupyter_code>def my_show(img, size=(3,3)):
    plt.figure(figsize=size)
    plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
    plt.show()
def my_show_HSV(img, size=(3,3)):
    plt.figure(figsize=size)
    plt.imshow(cv2.cvtColor(img,cv2.COLOR_HSV2RGB))
    plt.show()
def my_show_gray(img, size=(3,3)):
    plt.figure(figsize=size)
    plt.imshow(img,cmap='gray')
    plt.show()
img=cv2.imread('Herman.jpg')
rows,cols,channels = img.shape
img=cv2.resize(img,None,fx=0.5,fy=0.5)
rows,cols,channels = img.shape
my_show(img)<jupyter_output><empty_output><jupyter_text>Mask creation, fixed with dilation and erosion<jupyter_code>hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_blue=np.array([78,43,46])
upper_blue=np.array([110,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
my_show_gray(mask)
print('mask.shape: ',mask.shape)
print('mask: ',mask)
# mask is only 2D, value is 255 or 0
num = 1 # choose number of iterations
def fix(mask,iteration,depth):
    for i in range (0,num+1):
        mask=cv2.erode(mask,None,iterations=depth)
        #erode=cv2.erode(mask,None,iterations=1)
        #cv2.imshow('erode',erode)
        mask=cv2.dilate(mask,None,iterations=depth)
        #dilate=cv2.dilate(erode,None,iterations=1)
        #cv2.imshow('dilate',dilate)
    return mask
fixed_mask = fix(mask,1,3)
fixed_mask.shape
fixed_mask = fix(mask,2,5)
my_show_gray(fixed_mask)<jupyter_output><empty_output><jupyter_text>From above, we see 3 erode -> 3 dilate is good enough for this picture  Now, change the background<jupyter_code>for i in range(rows):
    for j in range(cols):
        if fixed_mask[i,j]==255:
            img[i,j]=(0,0,255)#img is still BGR channel
my_show(img)<jupyter_output><empty_output><jupyter_text>The following is work done by other classmates:  (Haven't appreciate yet)<jupyter_code>img=cv2.imread('Herman.jpg')
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# 敏感度
sensitivity = 15
# 定义蓝色在hsv的范围
lower_blue = np.array([85-sensitivity,100,100])
upper_blue = np.array([85+sensitivity,255,255])
# 这里有个坑，HSV里蓝色H值是240，然后在cv2里H值减半是120，zhihu上代码按120+-来做本来是OK的。
# 但是我选的这个ID照片原始是湖蓝背景，转成hsv后，见上图，可以看到人脸及眼睛遮层变成蓝了，反而背景蓝偏绿了，我们的目的是要把背景换掉，因此
# 用zhihu这个怎么扣都扣的人脸！！！因此找了个colorPicker的工具看了下hsv图里现在这个蓝绿背景的H值，改成85后，背景和人就顺利分开了
mask_blue = cv2.inRange(img_hsv,lower_blue,upper_blue)

# 这一段算完是把背景和前景分开了，（5，5）是平滑度，更小的话，可以分的更细腻，为什么这么算就看懂
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
mask_blue = cv2.morphologyEx(mask_blue, cv2.MORPH_CLOSE, kernel)  # 闭运算
mask_blue = cv2.morphologyEx(mask_blue, cv2.MORPH_OPEN, kernel)   # 开运算
# 算完背景是黑255，前景是白0
print(mask_blue)
# cv2.imshow('aaa',mask_blue)
# if cv2.waitKey(0)==27:
#     cv2.destroyAllWindows()
H,S,V = cv2.split(img_hsv)
print(H[0][0])
new_mask_blue = mask_blue
new_mask_blue[new_mask_blue==255] = 25
print(new_mask_blue)
def img_bgColorChange(img,mask,destColorH):
    H,S,V = cv2.split(img)
    print(mask)
    mask[mask==255] = destColorH
    # H[(bg-sensitivity)<H<(bg+sensitivity)]=destColorH
    print(mask)
    H=H+mask
    print(H)
    return cv2.merge((H,S,V))
#     print(H)
#     H=H+mask_blue
#     print(H)
#     H[H>255] = (H[H>255]-255-(srcColorH-destColorH)).astype(img.dtype)

new_img_hsv = img_bgColorChange(img_hsv,mask_blue,40)
my_show_HSV(new_img_hsv)
new_mask_blue = mask_blue
new_mask_blue[new_mask_blue==255] = 25
print(new_mask_blue)

H,S,V = cv2.split(img_hsv)
H = H +new_mask_blue
my_show_HSV(cv2.merge((H,S,V)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14705882352941177, 'fraction_numerical': 0.043689320388349516, 'mean_word_length': 6.207818930041152, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '551693', 'license': 'MIT', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1701, 'n_words': 201, 'path': 'chap2/bg_change.ipynb', 'repo_name': 'DDQXZcp/custom_medianBlur', 'size': '3502'}