[Python] How to use OpenCV (Python) to change specific colors without loss of quality?

Discussão em 'Python' iniciado por Stack, Junho 19, 2025.

  1. Stack

    Stack Membro Participativo

    I have this floor plan (link removed)

    And I want to change its colors so I can use OCR to read the room numbers. To do this, I want to do the following: change all red to white, and every other color to black, so all that is left is the room numbers. I wanted to try thresholding, but I saw on the documentation that it should only be done on grayscale images, so I first ran the following code to grayscale it:

    import cv2
    import os
    from ConvertSVG import svg_2_png

    # Convert the SVG to a PNG
    output_path = os.path.join('converted svgs', 'Level 3.png')

    svg_2_png(os.path.join('svg', 'Level 3.svg'), output_path)

    img = cv2.imread(output_path)
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    cv2.imshow("Grayscale", gray_image)

    cv2.waitKey(0)


    And I got this output (link removed)

    As you can see, the grayscale did work, but the room numbers got blockier and harder for OCR to read.

    How could I use OpenCV-python to change all red to white, and every other color to black, with as little "blockage" as possible?

    Continue reading...

Compartilhe esta Página