1

I'm trying to understand the Image Processing using Python in order to speed my workflow.

I've seen few services like Clipping path, Remove.bg that offer good results in terms of removing the background or the "unwanted" parts of an image (in my case, the mannequin parts: arms, neck, bottom part) and I'm wondering if there's a way to achieve some "workable" results using Python ?

Original image to test

Original image to test

Here the result I get using a the code below that was found here and adjusted a bit.

I noticed that I get better results by using original image size, even if it's a bit slower to process it.

Is Python capable of doing this consistently without much human interaction or I'm a dreamer ?

enter image description here

import cv2
import numpy as np

#== Parameters =======================================================================
BLUR = 21
CANNY_THRESH_1 = 110
CANNY_THRESH_2 = 200
MASK_DILATE_ITER = 10
MASK_ERODE_ITER = 10
MASK_COLOR = (0.0,0.0,1.0) # In BGR format


#== Processing =======================================================================

#-- Read image -----------------------------------------------------------------------
img = cv2.imread('teee.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#-- Edge detection -------------------------------------------------------------------
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges, None)
edges = cv2.erode(edges, None)

#-- Find contours in edges, sort by area ---------------------------------------------
contour_info = []
_, contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# Previously, for a previous version of cv2, this line was: 
#  contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# Thanks to notes from commenters, I've updated the code but left this note
for c in contours:
    contour_info.append((
        c,
        cv2.isContourConvex(c),
        cv2.contourArea(c),
    ))
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
max_contour = contour_info[0]

#-- Create empty mask, draw filled polygon on it corresponding to largest contour ----
# Mask is black, polygon is white
mask = np.zeros(edges.shape)
cv2.fillConvexPoly(mask, max_contour[0], (255))

#-- Smooth mask, then blur it --------------------------------------------------------
mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
mask_stack = np.dstack([mask]*3)    # Create 3-channel alpha mask

#-- Blend masked img into MASK_COLOR background --------------------------------------
mask_stack  = mask_stack.astype('float32') / 255.0          # Use float matrices, 
img         = img.astype('float32') / 255.0                 #  for easy blending

masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR) # Blend
masked = (masked * 255).astype('uint8')                     # Convert back to 8-bit 

c_red, c_green, c_blue = cv2.split(img)

img_a = cv2.merge((c_red, c_green, c_blue, mask.astype('float32') / 255.0))
cv2.imwrite('tee_edited.png', img_a*255)
nathancy
  • 42,661
  • 14
  • 115
  • 137
Andie31
  • 305
  • 3
  • 13
  • This is a really good start to a question, but I think it's a bit broad to really help you specifically. I feel like this is more of a general "computer vision technique" recommendation question – GPPK May 14 '19 at 14:37
  • Ohh sorry...what exactly looks broad to you @GPPK ? – Andie31 May 14 '19 at 14:40
  • 1
    Well python can do anything consistently, the problem you are going to have is whether your images are going to consistently work with whatever algorithm you implement. You can see your current algorithm does a pretty good job with that picture. But what about different pictures, different lighting, different backgrounds/tshirt colours. IMO that is the issue you are now facing, rather than implementation, which you've shown you can go some way to achieving – GPPK May 14 '19 at 14:43
  • That make a lot of sens ! You're right ! But those services mentioned by me, somehow are capable of achieving very good results with different conditions...I mean...that's why they're not free...Thank you very much for your explanation ! – Andie31 May 14 '19 at 14:48
  • 1
    exactly, they will have months of person-effort behind their services. – GPPK May 14 '19 at 14:50

0 Answers0