You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python优化从黑底白边框图片批量提取水果形状?

优化后的水果形状提取方案

针对黑背景、带白色边框的水果形状提取需求,原代码存在阈值固定、轮廓层级冗余、未针对性筛选有效轮廓等问题,以下是优化后的实现:

优化思路

  • 用Otsu自动阈值替代固定阈值,适配不同亮度的图片
  • 提取最外层轮廓(RETR_EXTERNAL),避免内部小轮廓干扰
  • 过滤无效轮廓(比如面积过小的噪声)
  • 优化掩码生成与透明背景处理,确保提取结果边缘清晰

优化代码

import cv2
import numpy as np
from PIL import Image
import os

# 创建输出目录
output_dir = 'extracted_fruits'
os.makedirs(output_dir, exist_ok=True)

# 读取图片
image_path = 'shapes.png'
img = cv2.imread(image_path)
if img is None:
    raise ValueError(f"无法读取图片: {image_path}")

# 转灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Otsu自动阈值分割(适配黑背景白边框的场景)
_, threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# 提取最外层轮廓,减少层级干扰
contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# 处理每个轮廓,过滤无效噪声
min_contour_area = 100  # 根据实际水果大小调整
for idx, contour in enumerate(contours):
    area = cv2.contourArea(contour)
    if area < min_contour_area:
        continue  # 跳过小噪声轮廓

    # 获取轮廓的外接矩形,精准定位水果区域
    x, y, w, h = cv2.boundingRect(contour)
    roi = img[y:y+h, x:x+w]
    roi_gray = gray[y:y+h, x:x+w]

    # 生成掩码,去掉白色边框(如果需要提取水果本身而非边框)
    _, roi_thresh = cv2.threshold(roi_gray, 240, 255, cv2.THRESH_BINARY_INV)
    mask = cv2.merge([roi_thresh, roi_thresh, roi_thresh])

    # 提取水果区域,去掉边框和黑背景
    fruit_img = cv2.bitwise_and(roi, mask)

    # 创建透明背景
    b, g, r = cv2.split(fruit_img)
    alpha = np.where(np.all(fruit_img == 0, axis=2), 0, 255).astype(np.uint8)
    rgba = cv2.merge((b, g, r, alpha))

    # 保存为PNG(带透明背景)
    pil_img = Image.fromarray(rgba)
    pil_img.save(f"{output_dir}/fruit_{idx}.png")

    # 可选:在原图上绘制轮廓
    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)

# 显示处理后的原图
cv2.imshow('Extracted Fruits', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

关键优化点说明

  • Otsu阈值:自动计算最优阈值,避免固定127导致的过分割/欠分割问题
  • RETR_EXTERNAL轮廓:只提取最外层的水果轮廓,不会拿到内部的小细节轮廓
  • 轮廓面积过滤:去掉图片中的噪声点,只保留有效水果形状
  • 边框去除:通过二次阈值分割去掉白色边框,精准提取水果本身(若要保留边框,可跳过掩码处理部分)
  • 透明背景优化:直接通过np.where生成alpha通道,边缘更干净,无残留黑像素

内容的提问来源于stack exchange,提问作者DevelopmentLLC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 19:31:00