[Python] python cv2 replace color with white

Discussão em 'Python' iniciado por Stack, Março 11, 2025.

  1. Stack

    Stack Membro Participativo

    I try to replace the turquoise part (words) from the image with white background to have a clear source for tesseract-ocr. The picture is loaded ok, image mask is created ok. My question is how to calculate (bidwise_and/or/xor) the right way, to have the turquoise part replaced with white. My code replace best case the turquoise part with black. Any idea is welcome and thank you for looking into.

    import cv2
    import numpy as np

    # Read image
    image = cv2.imread('pic1.png')

    # Show Image
    cv2.imshow('Pic1', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # Create white image
    whtimg = np.zeros([image.shape[0],image.shape[1],image.shape[2]],dtype=np.uint8)
    whtimg.fill(255)

    # Convert to HSV
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    whtimg = cv2.cvtColor(whtimg, cv2.COLOR_BGR2HSV)

    # Color Range - turquoise (color pick HSV: 165/77/87)
    lower_color = np.array([int(165/360*255-100), int(77/100*255*0.1), int(87/100*255-10)])
    upper_color = np.array([int(165/360*255+100), int(77/100*255*3.0) , int(87/100*255+10)])

    # Create mask to only select white
    mask1 = cv2.inRange(hsv, lower_color, upper_color)
    # mask1 = 255 - mask1 # invert mask

    # Show Mask
    cv2.imshow('Mask', mask1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # replace with white
    res = cv2.bitwise_or(hsv,whtimg,mask=mask1) # or bitwise_and, bitwise_xor

    # Convert to BGR
    res = cv2.cvtColor(res, cv2.COLOR_HSV2BGR)

    # Show Result
    cv2.imshow('Result', res)
    cv2.waitKey(0)
    cv2.destroyAllWindows()



    [​IMG]

    Continue reading...

Compartilhe esta Página