is there a way to get a more clear and successive edge with opencv in python?

Jay*_*Jay 2 python algorithm opencv machine-learning deep-learning

i am trying to make an edge detection that generate right image from left image in the flowing figure.

在此处输入图片说明

there are 3 different colorful area, so the result has 3 separated parts.

here is my code

img = cv2.imread('img2.png')
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
f, axs = plt.subplots(1,2,figsize=(12,8))
edges = cv2.Canny(img,1,255)
axs[0].imshow(imgRGB)
axs[1].imshow(edges,cmap = 'gray')
Run Code Online (Sandbox Code Playgroud)

here is the output

在此处输入图片说明

the edge of the red part in the origin image is being ignored by OpenCV, which does not meet my need.

the square edge is also being discarded, which does not meet my need.

the detected edges in the right Axes is not successive, which does not meet my need.

is there a way to fix above 3 bugs? or how to implement an new algorithm do meet my need.

J.D*_*.D. 5

You can improve the result by changing the values for Canny. Because your image consists of areas with similar color you can find the edges really good:

在此处输入图片说明

You can use this code to try for yourself:

import cv2
import numpy as np

cv2.namedWindow('Result')
img = cv2.imread('qkEuE.png')

v1 = 0
v2 = 0

def doEdges():
    edges = cv2.Canny(img,v1,v2)
    edges = cv2.cvtColor(edges,cv2.COLOR_GRAY2BGR)
    res = np.concatenate((img,edges),axis = 0)
    cv2.imshow('Result',res)
def setVal1(val):
    global v1
    v1 = val
    doEdges()
def setVal2(val):
    global v2
    v2 = val
    doEdges()

cv2.createTrackbar('Val1','Result',0,500,setVal1)
cv2.createTrackbar('Val2','Result',0,500,setVal2)

cv2.imshow('Result',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)