DAY 18
0

## 圖片逐位元運算基礎概念

10010110<=150
11001000<=200
10000000<=(150,200)逐位元運算結果=128

## 逐位元與運算cv2.bitwise_and()

``````#image_calculator.py
import cv2 as cv
import numpy as np

img3 = cv.bitwise_and(img,img2)#逐位元and邏輯運算

cv.imwrite('./img.png',img)
cv.imwrite('./img2.png',img2)
cv.imwrite('./img3.png',img3)
``````

## 逐位元或運算cv2.bitwise_or()

``````from cv2 import cv2 as cv
import numpy as np

img3 = cv.bitwise_or(img,img2)#逐位元or邏輯運算

cv.imwrite('./img.png',img)
cv.imwrite('./img2.png',img2)
cv.imwrite('./img3.png',img3)
``````

## 逐位元非運算cv2.bitwise_not()

``````from cv2 import cv2 as ![https://ithelp.ithome.com.tw/upload/images/20201002/201211765A7QxEhPkQ.png](https://ithelp.ithome.com.tw/upload/images/20201002/201211765A7QxEhPkQ.png)cv
import numpy as np

img3 = cv.bitwise_not(img)#逐位元not邏輯運算1
img4 = cv.bitwise_not(img2)#逐位元not邏輯運算2
cv.imwrite('./img.png',img)
cv.imwrite('./img2.png',img2)
cv.imwrite('./img3.png',img3)
cv.imwrite('./img4.png',img4)
``````

## 逐位元互斥運算cv2.bitwise_xor()

01111011<=123
11100111<=231
10011100<=互斥運算結果=156

``````from cv2 import cv2 as cv
import numpy as np

img3 = cv.bitwise_xor(img,img2)#逐位元xor邏輯運算

cv.imwrite('./img.png',img)
cv.imwrite('./img2.png',img2)
cv.imwrite('./img3.png',img3)
``````

## 遮罩處理

``````from cv2 import cv2 as cv
import numpy as np

cv.imwrite('./img.png',img)
cv.imwrite('./img2.png',img2)
cv.imwrite('./img3.png',img3)
``````