使用python检测图像上的白色背景 [英] Detect white background on images using python

查看:1192
本文介绍了使用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.

推荐答案

我会喜欢这样的东西.

  1. 通过将最亮,最白的像素设置为240(而不是255)来降低图像的对比度,这样通常在图像中和产品的某些部分中发现的白色不再是纯白色.

  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.

在图像周围放置1像素宽的白色边框-这将使下一步的泛洪处理一直沿边缘流" 进行(即使 产品" 触摸框架的边缘),然后倾斜" 从所有边界/边缘进入图像.

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.

从左上角开始填充图像(在步骤2之后必须为纯白色),并且在与白色匹配时允许容差为10-20%,以防背景为灰白色或稍有阴影,白色将在边缘的所有区域流入您的图像,直到到达中间的产品为止.

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.

我会像这样从命令行使用ImageMagick:

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

我将在以下2张图片周围放置一个红色边框,以便您可以看到StackOverflow白色背景上的边缘,并向您展示之前和之后的图像-查看生成的图像中白色的数量(第二个是因为它没有白色背景),也位于路由器下方的阴影处,以查看-fuzz的效果.

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.

之前

之后

如果要以百分比表示,可以将所有非白色像素设为黑色,然后按以下方式计算白色像素的百分比:

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

之前

之后

ImageMagick具有Python绑定,因此您可以在Python中执行上述操作-或者您可以使用OpenCV和Python来实现相同的算法.

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

这篇关于使用python检测图像上的白色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆