Detect white background on images using python

北城以北 提交于 2019-12-11 01:04:54

问题


Is there a way to tell whether an image as a white background using python and what could be a good strategy to get a "percentage of confidence" about this question? Seems like the literature on internet doesn't cover exactly this case and I can't find anything strictly related.

The images I want to analyze are typical e-commerce website product pictures, so they should have a single focused object in the middle and white background only at the borders.

Another information that could be available is the max percentage of image space the object should occupy.


回答1:


I would go with something like this.

  1. Reduce the contrast of the image by making the brightest, whitest pixel something like 240 instead of 255 so that the whites generally found within the image and within parts of the product are no longer pure white.

  2. Put a 1 pixel wide white border around your image - that will allow the floodfill in the next step to "flow" all the way around the edge (even if the "product" touches the edges of the frame) and "seep" into the image from all borders/edges.

  3. Floofdill your image starting at the top-left corner (which is necessarily pure white after step 2) and allow a tolerance of 10-20% when matching the white in case the background is off-white or slightly shadowed, and the white will flow into your image all around the edges until it reaches the product in the centre.

  4. See how many pure white pixels you have now - these are the background ones. The percentage of pure white pixels will give you an indicator of confidence in the image being a product on a whitish background.

I would use ImageMagick from the command line like this:

convert product.jpg +level 5% -bordercolor white -border 1 \
  -fill white -fuzz 25% -draw "color 0,0 floodfill" result.jpg

I will put a red border around the following 2 pictures just so you can see the edges on StackOverflow's white background, and show you the before and after images - look at the amount of white in the resulting images (there is none in the second one because it didn't have a white background) and also at the shadow under the router to see the effect of the -fuzz.

Before

After

If you want that as a percentage, you can make all non-white pixels black and then calculate the percentage of white pixels like this:

convert product.jpg -level 5%                                      \
   -bordercolor white -border 1                                    \
   -fill white -fuzz 25% -draw "color 0,0 floodfill" -shave 1      \
   -fuzz 0 -fill black +opaque white -format "%[fx:int(mean*100)]" info:
62

Before

After

ImageMagick has Python bindings so you could do the above in Python - or you could use OpenCV and Python to implement the same algorithm.




回答2:


The most popular image format is .png. PNG image can have a transparent color (alpha). Often match with the white background page. With pillow is easy to find out which pixels are transparent.

A good starting point:

from PIL import Image

img = Image.open('image.png')
img = img.convert("RGBA")

pixdata = img.load()

for y in xrange(img.size[1]):
    for x in xrange(img.size[0]):
        pixel = pixdata[x, y]
        if pixel[3] == 255:
           # tranparent....

Or maybe it's enough if you check if top-left pixel it's white:

pixel = pixdata[0, 0]
if item[0] == 255 and item[1] == 255 and item[2] == 255:
   # it's white


来源:https://stackoverflow.com/questions/36982097/detect-white-background-on-images-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!