Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edge Detection and transparency

Using images of articles of clothing taken against a consistent background, I would like to make all pixels in the image transparent except for the clothing. What is the best way to go about this? I have researched the algorithms that are common for this and the open source library opencv. Aside from rolling my own or using opencv is there an easy way to do this? I am open to any language or platform.

Thanks

like image 315
Joe Cannatti Avatar asked Oct 26 '09 19:10

Joe Cannatti


2 Answers

If your background is consistend in an image but inconsistent across images it could get tricky, but here is what I would do:

  1. Separate the image into some intensity/colour form such as YUV or Lab.
  2. Make a histogram over the colour part. Find the most occuring colour, this is (most likely) your background (update) maybe a better trick here would be to find the most occuring colour of all pixels within one or two pixels from the edge of the image.
  3. Starting from the eddges of the image, set all pixels that have that colour and are connected to the edge through pixels of that colour to transparent.
  4. The edge of the piece of clothing is now going to look a bit ugly because it consist of pixels that gain their colour from both the background and the piece of clothing. To combat this you need to do a bit more work:

    1. Find the edge of the piece of clothing through some edge detection mechanism.
    2. Replace the colour of the edge pixels with a blend of the colour just "inside" the edge pixel (i.e. the colour of the clothing in that region) and transparent (if your output image format supports that).
    3. If you want to get really fancy, you increase the transparency depending on how much "like" the background colour the colour of that pixel is.
like image 166
jilles de wit Avatar answered Oct 30 '22 22:10

jilles de wit


Basically, find the color of the background and subtract it, but I guess you knew this. It's a little tricky to do this all automatically, but it seems possible.

First, take a look at blob detection with OpenCV and see if this is basically done for you.

To do it yourself:

find the background: There are several options. Probably easiest is to histogram the image, and the large number of pixels with similar values are the background, and if there are two large collections, the background will be the one with a big hole in the middle. Another approach is to take a band around the perimeter as the background color, but this seems inferior as, for example, reflection from a flash could dramatically brighten more centrally located background pixels.

remove the background: a first take at this would be to threshold the image based on the background color, and then run the "open" or "close" algorithms on this, and then use this as a mask to select your clothing article. (The point of open/close is to not remove small background colored items on the clothing, like black buttons on a white blouse, or, say, bright reflections on black clothing.)

OpenCV is a good tool for this.

The trickiest part of this will probably be at the shadow around the object (e.g. a black jacket on a white background will have a continuous gray shadow at some of the edges and where to make this cut?), but if you get this far, post another question.

like image 34
tom10 Avatar answered Oct 30 '22 21:10

tom10