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

OpenCV图像目标捕获遗漏问题:代码及图像异常求助

目标对象捕获遗漏问题解决方案

问题描述

需要捕获图像中的目标对象,但无法定位到所需对象,原图像左下角区域频繁被遗漏,部分细节丢失,现有代码无法有效解决该问题。

相关图像

  • 检测前图像
  • 检测后图像

现有代码

import cv2
import os

def crop_images(input_path, output_dir):
  
    image = cv2.imread(input_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5, 5), 0)

    _, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
    
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (11, 11))
    dilate = cv2.dilate(thresh, kernel, iterations=1)
    
    cnts, _ = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    image_with_rectangles = image.copy()  
    
    image_number = 0
    min_roi_size = 7000
    max_roi_size = 30000
    for c in cnts:
        x, y, w, h = cv2.boundingRect(c)
        
        if max_roi_size > w * h > min_roi_size:
            cv2.rectangle(image_with_rectangles, (x, y), (x + w, y + h), (0, 255, 0), 2) 
            ROI = image[y:y + h, x:x + w]
            output_path = os.path.join(output_dir, f"ROI_{image_number}.jpg")
            cv2.imwrite(output_path, ROI)
            image_number += 1

    cv2.imwrite("image_with_rectangles.jpg", image_with_rectangles)

针对性调整方案

1. 优化预处理与阈值逻辑

左下角区域可能因局部亮度差异导致阈值失效,可调整如下:

  • 替换高斯模糊为中值模糊,保留边缘细节同时过滤噪声:
    blur = cv2.medianBlur(gray, 5)
    
  • 改用自适应阈值替代OTSU全局阈值,适配局部亮度变化:
    thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
    

2. 调整形态学操作参数

当前膨胀核过大,易忽略小目标或角落区域:

  • 缩小膨胀核尺寸并减少迭代次数:
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    dilate = cv2.dilate(thresh, kernel, iterations=1)
    
  • 或使用开运算(先腐蚀后膨胀),去除噪点同时保留目标轮廓:
    erode = cv2.erode(thresh, kernel, iterations=1)
    dilate = cv2.dilate(erode, kernel, iterations=1)
    

3. 修改轮廓检测模式

当前cv2.RETR_EXTERNAL仅检测最外层轮廓,若左下角目标被遮挡会被遗漏,改用全层级轮廓检测:

cnts, _ = cv2.findContours(dilate, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

4. 优化ROI过滤规则

左下角目标可能尺寸偏小,超出当前尺寸限制:

  • 降低最小ROI尺寸阈值,比如调整为:
    min_roi_size = 3000
    max_roi_size = 30000
    
  • 增加位置判断,单独放宽左下角区域的尺寸限制:
    h, w = image.shape[:2]
    for c in cnts:
        x, y, rect_w, rect_h = cv2.boundingRect(c)
        area = rect_w * rect_h
        # 针对左下1/4区域单独处理
        if (x < w//2 and y > h//2):
            if area > 1000:  # 更低的尺寸阈值
                cv2.rectangle(image_with_rectangles, (x, y), (x+rect_w, y+rect_h), (0,0,255), 2)
                ROI = image[y:y+rect_h, x:x+rect_w]
                # 保存ROI逻辑...
        else:
            if max_roi_size > area > min_roi_size:
                # 原逻辑处理
    

5. 增加亮度对比度补偿

对图像先做亮度调整,解决左下角偏暗问题:

# 调整亮度与对比度,alpha为对比度增益,beta为亮度偏移
alpha = 1.5
beta = 30
adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
gray = cv2.cvtColor(adjusted, cv2.COLOR_BGR2GRAY)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:05:21