Removing the background of an image - python

Asked

Viewed 1,234 times

2

I’m developing a code to remove the background of an image, I’m trying to use the Canny border detector to elaborate a mask and try to remove a piece of pipe from the image:

Tubulação

The code I’m working on is this:

img = imread('Imagem.jpg')
gray = cvtColor(img,COLOR_BGR2GRAY)

edges = Canny(gray,10,100) #Utiliza o metodo Canny
edges = dilate(edges, None) # Dilata as linhas da fronteira
edges = erode(edges, None) # remove pequenos "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')

being the result:

inserir a descrição da imagem aqui

I tried to adjust the gradients of the function cv2.Canny, but only gets worse, someone could help me?

  • 1

    Have a look: http://stackoverflow.com/a/29314286/664577

  • Have you taken a look at the image that results from the Canny application? It can help you fine-tune the parameters of this call. What interests you are the edges between the pipe piece and the rest of the image, and not the other possible existing edges (perhaps in the texture of the cardboard, but certainly between the white and brown of the box).

  • Another thing: the pipes always have different colors of the box where they are? If yes, you have tried one limiarization of the color histogram? May be enough for your problem.

  • Well, since you didn’t answer my comments, I will vote to close as not clear. I also leave the same ear tug that I have already left in another question (which, by the way, might even help you with the problem itself): if you don’t mind facilitating understanding, it’s hard for someone to be interested in helping you.

  • 1

    I was quite busy these days, so I could not read the comments properly, I will test the tips and when I get a solution I will put the code in this question so that others can use. Sorry and thank you Luiz Vieira

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.