简体   繁体   中英

line not showing on white backgroud CV2 python

I am trying to draw a line on a white background but the line is not showing in CV2.

import cv2
import numpy as np

while True:
    white = np.zeros([512,512,3])
    white.fill(255)
    canvas = np.zeros_like(white)
    canvas = cv2.line(canvas, (0, 0), (200, 200), (0, 0, 255), 5)
    white = cv2.add(canvas,white)#canvas is not showing
    cv2.imshow("white",white)

    key = cv2.waitKey(1)
    if key == ord("0"):
        break

same code is working with black background.

You are adding the canvas to the white background. So basically you are adding [0, 0, 255] (Red) to [255, 255, 255] (white) which is also white since no value can be above 255.

To archive the goal you have to subtract canvas from white.

Change

white = cv2.add(canvas,white)

To

white = cv2.substract(white, canvas)

Btw: this wouldnt work for a black background. To make sure it works with every background you would have to draw directly on the background instead of creating a canvas and substracting it from the original image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM