【问题标题】:Detect Main Object in Image?检测图像中的主要对象?
【发布时间】:2025-12-07 03:35:01
【问题描述】:

我有如下图所示的图片。我的第一个想法是从图像的角落开始使用洪水填充,制作一个蒙版,然后我可以稍后提取鞋子。 Opencv 或其他 Python 库有没有更好的方法?

【问题讨论】:

  • 您到底想要什么?您不能使用 Python 图像库 (PIL) 来检测所有白色像素吗?也许您还想研究 Canny 边缘检测 (opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/…)?
  • 我不想要边缘,我想要一个可以提取鞋子所有像素的蒙版。另外,我不知道背景是否永远是白色的
  • 好的,很清楚。不幸的是,我也无法进一步帮助您。 :-(
  • 别担心!希望之前有人遇到过这个问题
  • 活动轮廓可能是您正在寻找的(尽管它们很慢)。请参阅this answer 了解该做什么。虽然它适用于 Matlab,但我相信你可以找到 Python/OpenCV 类似的东西。

标签: python opencv image-processing numpy computer-vision


【解决方案1】:

在 Lyst 上有一篇关于这个问题的帖子。确切地说,我的意思是分割鞋子。这是link

基本上,他们使用的步骤是:

  1. 反转颜色
  2. 应用 Sobel 过滤器
  3. 应用高斯模糊
  4. 阈值
  5. 洪水填充

示例以python代码sn-p结尾。

在你的情况下,如果你总是有白色背景,你可能只使用阈值。

【讨论】:

    【解决方案2】:

    这里我用颜色值移除像素, 你可以修改它。

    from PIL import Image
    #here i am using 1.jpg to 40.jpg images
    #code will remove all pixel which is graterthan (245,245,245)
    for i in range(1,41):
        i = str(i)
        img = Image.open(i+'.jpg') #open image
        img = img.convert("RGBA")
        datas = img.getdata()
        newData = []
        for item in datas:
            if item[0] >= 245 and item[1] >= 245 and item[2] >= 245:    #check pixel color
                newData.append((255, 255, 255, 0))                   
            else:
                newData.append(item)
        i = int(i)
        print i 
        img.putdata(newData)
        i = str(i)
        #img = img.convert("LA") convert image in Grayscale
        img.save(i+'.png', "PNG") #save as png file
    

    【讨论】: