I am working on a code to remove the background of an image, I am trying to use the Canny border detector to elaborate a mask and try to remove a piece of pipe from the image:
ThecodeI'mworkingonisthis:
img=imread('Imagem.jpg')gray=cvtColor(img,COLOR_BGR2GRAY)edges=Canny(gray,10,100)#UtilizaometodoCannyedges=dilate(edges,None)#Dilataaslinhasdafronteiraedges=erode(edges,None)#removepequenos"buracos"
contour_info = []
contours, _ = findContours(edges, RETR_LIST, CHAIN_APPROX_NONE)
for c in contours:
contour_info.append((c, isContourConvex(c),contourArea(c), ))
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
max_contour = contour_info[0]
mask = np.zeros(edges.shape)
fillConvexPoly(mask, max_contour[0], (255))
mask_stack = np.dstack([mask]*3) # Cria uma mascara de 3 canais
mask_stack = mask_stack.astype('float32') / 255.0
img = img.astype('float32') / 255.0
Mscor = (1.0,1.0,1.0) # Formato BGR
masked = (mask_stack * img) + ((1-mask_stack) * Mscor)
masked = (masked * 255).astype('uint8')
The result being:
I've already tried adjusting the gradients of the cv2.Canny
function, but it only gets worse, could anyone help me?