Pin-Jiun / ComputerVision

0 stars 0 forks source link

14-cv2.bitwise to retouch #14

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago

image

先來看看我們今天的兩張圖

圖一 image

圖二 image

import cv2
import numpy as np
import matplotlib.pyplot as plt
import glob
from IPython.display import clear_output

def show_img(img):
    plt.style.use('dark_background')
#     plt.figure(figsize=(10,10)) 
    image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    plt.imshow(image_rgb)
    plt.show()

def draw_cirlces(img, circle_mid, color, radius):
    cv2.circle(img, circle_mid, radius, color, -1)

    return img

shape = (200, 200, 3)
img1 = np.zeros(shape, np.uint8)
img1 = draw_cirlces(img1, circle_mid=(80,80), color=(255,255,255) ,radius=50)

print("Picture 1 :")
show_img(img1)

shape = (200, 200, 3)
img2 = np.zeros(shape, np.uint8)
img2 = draw_cirlces(img2, circle_mid=(120,120), color=(255,0,0) ,radius=50)

print("Picture 2 :")
show_img(img2)

print("Picture 1 or 2 :")
img1_img2_or =cv2.bitwise_or(img1, img2, dst=None, mask=None)
show_img(img1_img2_or)

print("Picture 1 and 2 :")
img1_img2_and =cv2.bitwise_and(img1, img2, dst=None, mask=None)
show_img(img1_img2_and)

print("Picture 1 xor 2 :")
img1_img2_xor =cv2.bitwise_xor(img1, img2, dst=None, mask=None)
show_img(img1_img2_xor)

print("Picture 1 xor 2 :")
overlapping55 = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
show_img(overlapping55)

print("Picture 1 not :")
img1_not =cv2.bitwise_not(img1)
show_img(img1_not)

# ---------------- 印出結果圖表 ---------------- #

plt.figure(figsize=(15,15)) 

plt.subplot(3, 3, 1)
plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
plt.title("img1", {'fontsize':20})  

plt.subplot(3, 3, 2)
plt.imshow(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
plt.title("img2", {'fontsize':20})

plt.subplot(3, 3, 3)
img1_not =cv2.bitwise_not(img1)
plt.imshow(cv2.cvtColor(img1_not, cv2.COLOR_BGR2RGB))
plt.title("not img1", {'fontsize':20})

plt.subplot(3, 3, 4)
img1_img2_or =cv2.bitwise_or(img1, img2, dst=None, mask=None)
plt.imshow(cv2.cvtColor(img1_img2_or, cv2.COLOR_BGR2RGB))
plt.title("img1 or img2", {'fontsize':20})

plt.subplot(3, 3, 5)
img1_img2_and =cv2.bitwise_and(img1, img2, dst=None, mask=None)
plt.imshow(cv2.cvtColor(img1_img2_and, cv2.COLOR_BGR2RGB))
plt.title("img1 and img2", {'fontsize':20})

plt.subplot(3, 3, 6)
img1_img2_xor =cv2.bitwise_xor(img1, img2, dst=None, mask=None)
plt.imshow(cv2.cvtColor(img1_img2_xor, cv2.COLOR_BGR2RGB))
plt.title("img1 xor img2", {'fontsize':20})

plt.subplot(3, 3, 7)
add_result = cv2.add(img1, img2)
plt.imshow(cv2.cvtColor(add_result, cv2.COLOR_BGR2RGB))
plt.title("Add (img1 + img2)", {'fontsize':20})

plt.subplot(3, 3, 8)
subtract_result = cv2.subtract(img1, img2) 
plt.imshow(cv2.cvtColor(subtract_result, cv2.COLOR_BGR2RGB))
plt.title("Subtract (img1 - img2)", {'fontsize':20})

plt.subplot(3, 3, 9)
overlapping55 = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
plt.imshow(cv2.cvtColor(overlapping55, cv2.COLOR_BGR2RGB))
plt.title("img1 and img2 = 5:5", {'fontsize':20})

plt.show()

我們如果真的要實現把一個人P到另外一張圖, 我們直接用 addWeighted 是行不通的, 因為 addWeighted 只有調整透明度後貼上去, 因此貼上去的圖片原本的背景一定還在。

那想P圖應該怎麼做呢? 先將圖片做二值化(之後的內容會提到,簡單來說將圖片變為黑白色)。 把要挖掉的地方先用成黑色,可能會用到 cv2.bitwise_not 使用 cv2.bitwise_and 挖掉該區域 (因為黑色=0全挖空,白色=255全保留) 用 cv2.add 將圖片貼上該空白(被挖掉的)區域 (因為黑色=0,相加沒影響)

https://www.wongwonggoods.com/all-posts/python/python_opencv/opencv-bitwise_or-and-xor-not/#cv2bitwise_or_%E5%9C%96%E7%89%87%E8%81%AF%E9%9B%86