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

含背景噪声的暗图中雨刮器边缘检测技术求助

雨刮器边缘自动识别方案求助

我有大量高速相机拍摄的图像需要分析,第一步需识别雨刮器的边缘,但雨刮器在图像中呈黑色(负向显示),自动识别存在困难。

我已尝试使用OpenCV的Canny+HoughLinesP组合、Sobel梯度转换后再用Canny+HoughLinesP的方法,也调整过HoughLines的阈值,但存在以下问题:

  • 误检大量无关线条
  • 漏检目标线条
  • 检测出的线条不贴合边缘
  • 需手动调整单张图像阈值,自动调整阈值时常无法包含目标线条

理想效果是自动输出左右两条竖线,隐藏中间边缘,且无需手动适配所有图像。求可行实现方案或更合适的工具/库?

尝试的代码示例

import cv2
import numpy as np
from matplotlib import pyplot as plt

def is_vertical_line(x1, y1, x2, y2, angle_threshold=5):
    """检查线条是否在指定角度范围内垂直"""
    angle = np.degrees(np.arctan2(y2 - y1, x2 - x1))
    angle = abs(angle) % 180
    return abs(angle - 90) <= angle_threshold


image = cv2.imread('F:\Studienarbeit\Testdaten\SourceDaten\DEG_30kgh_8mbar_250rpm_110grad_HD2bar_Img000087.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
abs_sobelx = np.absolute(sobelx)
sobelx_8u = np.uint8(abs_sobelx)
edges = cv2.Canny(sobelx_8u, 5, 250, apertureSize=3)


lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=5, minLineLength=250, maxLineGap=40)

image_with_lines = image.copy()
if lines is not None:
    for line in lines:
        x1, y1, x2, y2 = line[0]
        if is_vertical_line(x1, y1, x2, y2):
            cv2.line(image_with_lines, (x1, y1), (x2, y2), (0, 0, 255), 2)


plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(cv2.convertScaleAbs(sobelx_8u), cmap='gray')
plt.title('Sobel Gradient')

plt.subplot(1, 2, 2)
plt.imshow(cv2.cvtColor(image_with_lines, cv2.COLOR_BGR2RGB))
plt.title('HoughLinesP')

plt.show()

图像说明

  • 雨刮器移动序列图:展示雨刮器在图像中从左到右移动的多帧状态(首尾图间有7张未展示)
  • 轻微旋转图像:雨刮器存在轻微角度旋转的拍摄图像

解决方案建议

1. 自适应阈值预处理优化

雨刮器是深色竖条,用自适应阈值二值化替代固定阈值,可根据局部亮度自动调整,突出雨刮器区域:

# 替换原灰度处理后的步骤
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 自适应二值化,反向阈值突出深色区域
binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)

2. 形态学操作强化竖线特征

用垂直结构元素做形态学膨胀,把雨刮器零散边缘连接成连续竖条,抑制噪点:

# 创建适配雨刮器尺寸的垂直结构元素
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 30))
# 膨胀强化竖线特征
vertical_lines = cv2.morphologyEx(binary, cv2.MORPH_DILATE, vertical_kernel)

3. 优化线条检测与聚类

  • 调整HoughLinesP参数:提高threshold(如20)过滤弱线条,根据雨刮器实际长度微调minLineLength;
  • 线条聚类去干扰:把检测到的垂直线条按x坐标聚类为2组,取每组最长线条作为左右边缘:
if lines is not None:
    x_midpoints = []
    line_coords = []
    for line in lines:
        x1, y1, x2, y2 = line[0]
        if is_vertical_line(x1, y1, x2, y2):
            x_mid = (x1 + x2) // 2
            x_midpoints.append([x_mid])
            line_coords.append((x1, y1, x2, y2))
    
    if len(x_midpoints) >= 2:
        # K-means聚类为左右两类
        x_midpoints = np.array(x_midpoints, dtype=np.float32)
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
        _, labels, centers = cv2.kmeans(x_midpoints, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
        
        # 每类取最长线条
        def get_longest_line(lines_list):
            max_len = 0
            longest_line = None
            for (x1,y1,x2,y2) in lines_list:
                length = np.sqrt((x2-x1)**2 + (y2-y1)**2)
                if length > max_len:
                    max_len = length
                    longest_line = (x1,y1,x2,y2)
            return longest_line
        
        left_lines = [line_coords[i] for i in range(len(labels)) if labels[i] == 0]
        right_lines = [line_coords[i] for i in range(len(labels)) if labels[i] == 1]
        
        left_line = get_longest_line(left_lines)
        right_line = get_longest_line(right_lines)
        
        if left_line:
            cv2.line(image_with_lines, (left_line[0], left_line[1]), (left_line[2], left_line[3]), (0,0,255), 2)
        if right_line:
            cv2.line(image_with_lines, (right_line[0], right_line[1]), (right_line[2], right_line[3]), (0,0,255), 2)

4. 备选工具/库

  • YOLOv8:标注少量样本训练轻量化目标检测模型,直接检测雨刮器左右边缘,适配不同角度与光照场景;
  • OpenCV LineSegmentDetector(LSD):替代HoughLinesP,精准检测线条,减少误检,阈值调整成本更低。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 06:00:03