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

图像方格内手写字母提取不全的技术问题求助

问题说明

我需要识别图像中的方格并提取其中的手写字母,但目前只能获取到字母片段。尝试过先去除灰色边框,再将所有非白色像素转为亮蓝色,但无法彻底清除边框;同时因为图像是扫描件,存在随机泛黄斑点,影响处理效果。

现有处理代码
img = cv2.imread(img_path)
img_path = "img.png"

gray = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)

# 平滑边缘以辅助锐化
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# sharpen_kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpen_kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
sharpen = cv2.filter2D(blur, -1, sharpen_kernel)

# 应用自适应阈值生成二值图像并闭合间隙
thresh = cv2.adaptiveThreshold(sharpen, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV, 11, 2)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)

# 检测轮廓并根据面积筛选
contours = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS)
contours = contours[0] if len(contours) == 2 else contours[1]
min_area = 350
max_area = 6200
image_number = 0
lower_blue = np.array([90, 50, 20])  
upper_blue = np.array([150, 255, 255]) 
# 过滤掉过小或过大的随机轮廓
contours = filter(lambda contour: min_area < cv2.contourArea(contour) < max_area, contours)
# 按水平位置排序轮廓,形成行结构
sorted_contours = sorted(contours, key=lambda contour: cv2.boundingRect(contour)[0])
selected_rects = []
for c in sorted_contours:
    x, y, w, h = cv2.boundingRect(c)
    # 确保当前矩形不与已选中的矩形重叠
    if not any(x > rect[0] and y > rect[1] and x + w < rect[0] + rect[2] and y + h < rect[1] + rect[3] for rect in selected_rects):
        ROI = image[y:y + h, x:x + w]
        ROI = cv2.GaussianBlur(ROI, (3, 3), 0)
        hsv = cv2.cvtColor(ROI, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, lower_blue, upper_blue)
        blue_pixels = cv2.countNonZero(mask)
        # 确保矩形内存在蓝色区域,排除无效轮廓
        if blue_pixels > 100:
            binary_image = cv2.resize(mask, (28, 28))
            cv2.imwrite(f'./resources/ROI_{image_number}.png', binary_image)
            image_number += 1
            selected_rects.append((x, y, w, h))
相关图像展示
  • 初始图像:初始图像
  • 部分提取的ROI:部分提取的ROI
  • 不完整的掩码:不完整的掩码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 20:03:19