pythonimage-processingcolor-detection

Detect white background on images using python


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.


Solution

  • 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

    enter image description here

    After

    enter image description here

    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
    

    enter image description here

    Before

    enter image description here

    After

    enter image description here

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