<button id="ssm6u"><optgroup id="ssm6u"></optgroup></button>
  • 跳轉至

    二值化邏輯運算

    概要

    本節, 阿凱創建了兩個二值化圖像, 演示了各種二值化運算對應的效果。并給出了詳細的二值化邏輯運算對應的真值表(Truth Table)。

    keywords 二值化 Binary Bool 邏輯運算

    1. 創建二值化圖像

    首先我們定義兩個圖形, 一個是正方形,另外一個為圓形。

    中間白色的區域是1 (灰度值為255)

    黑色的區域即為0 (灰度值為0)

    圖形1 正方形

    bitwise_rectangle

    rectangle = np.zeros((300, 300), dtype="uint8")
    cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1)
    cv2.imwrite("bitwise_rectangle.png", rectangle)
    

    圖形2 圓形

    bitwise_circle

    circle = np.zeros((300, 300), dtype="uint8")
    cv2.circle(circle, (150, 150), 150, 255, -1)
    cv2.imwrite("bitwise_circle.png", circle)
    

    完整的代碼如下:
    src/create-binary-image.py

    '''
    創建二值化的矩形還有圓形
    '''
    import cv2
    import numpy as np
    
    rectangle = np.zeros((300, 300), dtype="uint8")
    cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1)
    cv2.imwrite("bitwise_rectangle.png", rectangle)
    
    circle = np.zeros((300, 300), dtype="uint8")
    cv2.circle(circle, (150, 150), 150, 255, -1)
    cv2.imwrite("bitwise_circle.png", circle)
    

    然后我們對其進行邏輯運算。

    2. 邏輯非 - not

    邏輯非其實也相當于反色。 原來是白色的地方變成黑色, 原來是黑色的地方變成白色。

    bitwiseNOT = cv2.bitwise_not(circle)
    cv2.imwrite("bitwise_not_circle.png", bitwiseNOT)
    

    真值表

    A not A
    0 1
    1 0

    效果

    bitwise_not_circle

    完整源碼
    bitwise_not_circle.py

    '''
    測試二值化非
    '''
    import cv2
    
    circle = cv2.imread('bitwise_circle.png', cv2.IMREAD_GRAYSCALE)
    
    bitwiseNOT = cv2.bitwise_not(circle)
    cv2.imwrite("bitwise_not_circle.png", bitwiseNOT)
    

    3. 邏輯與 - and

    邏輯與經常被用于遮蓋層(MASK), 即去除背景, 選取自己感興趣的區域.

    bitwiseAnd = cv2.bitwise_and(rectangle, circle)
    cv2.imwrite("bitwise_and.png", bitwiseAnd)
    

    真值表

    A B A AND B
    0 0 0
    0 1 0
    1 0 0
    1 1 1

    效果

    bitwise_and

    完整源碼
    src/bitwise_and.py

    '''
    二值化圖像邏輯與
    '''
    import cv2
    
    circle = cv2.imread('bitwise_circle.png', cv2.IMREAD_GRAYSCALE)
    rectangle = cv2.imread('bitwise_rectangle.png', cv2.IMREAD_GRAYSCALE) 
    bitwiseAnd = cv2.bitwise_and(rectangle, circle)
    cv2.imwrite("bitwise_and.png", bitwiseAnd)
    

    4. 邏輯或 - or

    bitwiseOR = cv2.bitwise_or(rectangle, circle)
    cv2.imwrite("bitwise_or.png", bitwiseOR)
    

    真值表

    A B A OR B
    0 0 0
    0 1 1
    1 0 1
    1 1 1

    效果

    bitwise_or

    完整源碼

    bitwise_or.py

    '''
    二值化圖像-或
    '''
    import cv2
    
    circle = cv2.imread('bitwise_circle.png', cv2.IMREAD_GRAYSCALE)
    rectangle = cv2.imread('bitwise_rectangle.png', cv2.IMREAD_GRAYSCALE) 
    
    bitwiseOR = cv2.bitwise_or(rectangle, circle)
    cv2.imwrite("bitwise_or.png", bitwiseOR)
    

    5. 邏輯與非 - nand

    bitwiseNAnd = cv2.bitwise_not(bitwiseAnd)
    cv2.imwrite("bitwise_nand.png", bitwiseNAnd)
    

    真值表

    A B A NOT AND B
    0 0 1
    0 1 0
    1 0 0
    1 1 0

    效果

    bitwise_nand

    完整源碼

    bitwise_nand.py

    '''
    二值化圖像-與非
    '''
    import cv2
    
    circle = cv2.imread('bitwise_circle.png', cv2.IMREAD_GRAYSCALE)
    rectangle = cv2.imread('bitwise_rectangle.png', cv2.IMREAD_GRAYSCALE) 
    
    bitwiseAnd = cv2.bitwise_and(rectangle, circle)
    bitwiseNAnd = cv2.bitwise_not(bitwiseAnd)
    cv2.imwrite("bitwise_nand.png", bitwiseNAnd)
    

    6. 邏輯或非 - nor

    bitwiseNOR = cv2.bitwise_and(cv2.bitwise_not(rectangle), cv2.bitwise_not(circle))
    cv2.imwrite("bitwise_nor.png", bitwiseNOR)
    

    真值表

    A B A NOR B
    0 0 1
    0 1 0
    1 0 0
    1 1 0

    效果

    bitwise_nor

    7. 邏輯異或 - xor

    bitwiseXOR = cv2.bitwise_xor(rectangle, circle)
    cv2.imwrite("bitwise_xor.png", bitwiseXOR)
    

    真值表

    A B A OR B
    0 0 0
    0 1 1
    1 0 1
    1 1 0

    效果

    bitwise_xor

    完整源碼

    bitwise_xor.py

    '''
    二值化圖像-異或
    '''
    import cv2
    
    circle = cv2.imread('bitwise_circle.png', cv2.IMREAD_GRAYSCALE)
    rectangle = cv2.imread('bitwise_rectangle.png', cv2.IMREAD_GRAYSCALE) 
    
    bitwiseXOR = cv2.bitwise_xor(rectangle, circle)
    cv2.imwrite("bitwise_xor.png", bitwiseXOR)
    

    8. 邏輯異或非 - xnor

    bitwiseXNOR = cv2.bitwise_or(bitwiseAnd, bitwiseNOR)
    cv2.imwrite("bitwise_xnor.png", bitwiseXNOR)
    

    真值表

    A B A OR B
    0 0 1
    0 1 0
    1 0 0
    1 1 1

    效果

    bitwise_xnor

    完整源碼
    bitwise_xnor.py

    '''
    二值化圖像抑或非
    '''
    import cv2
    
    circle = cv2.imread('bitwise_circle.png', cv2.IMREAD_GRAYSCALE)
    rectangle = cv2.imread('bitwise_rectangle.png', cv2.IMREAD_GRAYSCALE) 
    
    bitwiseAnd = cv2.bitwise_and(rectangle, circle)
    bitwiseNOR = cv2.bitwise_not(cv2.bitwise_or(rectangle, circle))
    
    bitwiseXNOR = cv2.bitwise_or(bitwiseAnd, bitwiseNOR)
    cv2.imwrite("bitwise_xnor.png", bitwiseXNOR)
    


    韩国精品无码一区二区三区,精品无码一区二区三区AV,欧洲丰满美熟女乱又伦AV,亚洲午夜久久久影院伊人