python 白色怎么表示,使用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.

解决方案

I would go with something like this.

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.

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.

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.

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

91c92c2f67088ff46d74caa2bfb40f89.png

After

4ac7874e031537957acc00e6e396dcf4.png

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

3c3251dce9fdd880ad5db60cbc202006.png

Before

8ff694a4b27fbbe83a93830e63c68b64.png

After

7052a058ef3a9890505ec1362f9f8fd9.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值