Search code examples
pythonopencvcomputer-visionedge-detectioncanny-operator

Combine several Canny edge detection in one window using OpenCV python


I made three canny edge detection with different threshold values and then change the color line differently, now I want to combine all that lines to be displayed in one window. Here's a picture for example: Edge detection with different colors

And here's my code so far:

edges1 = cv2.Canny(frame,30,50)
rgb1 = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) # RGB for matplotlib, BGR for imshow() !

edges2 = cv2.Canny(frame,20,60)
rgb2 = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) 

edges3 = cv2.Canny(frame,40,40)
rgb3 = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB) 


# multiply with another array to change line colors:
rgb1 *= np.array((1,0,0),np.uint8) 
rgb2 *= np.array((0,1,0),np.uint8)
rgb3 *= np.array((0,0,1),np.uint8)



cv2.imshow('Deteksi Tepi dengan Canny', rgb1)

Solution

  • In Python/OpenCV, I believe you can do your Canny edge detection on your color frame image and the result will be one image with color edges.

    OpenCV uses color in BGR order. Matplotlib uses color in RGB order. But if you use RGB order, then cv2.imshow(will show the wrong colors). It needs BGR order.

    If frame is in color, just do

    canny = cv2.Canny(frame,30,50)
    

    Alternately, separate the channels from your frame and do the canny edge processing on each. Then combine the 3 canny results into each of the color channels.

    b,g,r = cv2.split(frame)
    b_edge = cv2.Canny(b,30,50)
    g_edge = cv2.Canny(g,30,50)
    r_edge = cv2.Canny(r,30,50)
    edge = cv2.merge([b_edge, g_edge, r_edge])
    cv2.imshow('Color Canny', edge)