Search code examples
pythonopencvedge-detectioncanny-operator

Is it possible to display colored text on a Canny Edge frame?


Please forgive me for the question, I am quite new to OpenCV. I am using canny edge detection on a people-counting program, and every time that a person walks into the frame an ID dot is placed in the center of the person. Before I applied code to generate the canny edges, I displayed the IDs as green, however, with the canny edges I can't see any color. Rather, the IDs are just outlines and nothing else. I would very much like to retain the color, as it would make it a lot easier to see with the black and white background.

Here is a picture of the frame that I have after Canny Edge has occurred

Here is a picture of the frame that I have after Canny Edge has occurred In the bottom left, you can see some text that I would like to be colored, but that has been changed by the edges.

Is it possible to display colored text in conjunction with Canny Edge detection?


Solution

  • I tested using opencv 4.0.1 and it works as expected: you can convert edge output image to bgr image, then put all the colored elements youy need. Here a small example how to do that:

        import cv2 as cv
        img = cv.imread(filename, cv.IMREAD_COLOR) 
        img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    
        filt = cv.medianBlur(img_gray, 9)
        filt = cv.blur(filt, (3, 3))
    
        edges = cv.Canny(filt, 10, 50)
    
        edges_bgr = cv.cvtColor(edges, cv.COLOR_GRAY2BGR)
        w,h = edges_bgr.shape[:2]
        center = (int(h/2), int(w/2))
        radius = 100
        cv.circle(edges_bgr, center, radius, (255,128,0), 3)
    
        font = cv.FONT_HERSHEY_SIMPLEX
        bottomLeftCornerOfText = center
        fontScale = 1
        lineType = 2
    
        cv.putText(edges_bgr,'Text with colors', 
                    bottomLeftCornerOfText, 
                    font, 
                    fontScale,
                    (0,128,255),
                    lineType)
    
        cv.imshow("edges with colors", edges_bgr)
    

    enter image description here