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

如何用Python OpenCV/PIL连接图像中距离最小的端点修复裁剪的数字2

嘿,作为新手能想到这种精准的修复思路真的挺厉害的!针对你要修复左侧裁切的数字2、只连接距离最近的一对端点的需求,我给你整理了一套用Python+OpenCV实现的步骤,都是基础操作,新手也能跟着走:

实现步骤详解

1. 图像预处理:转二值图+提取数字轮廓

首先得把图像转成黑白二值图,这样才能清晰地提取数字的轮廓——这是找端点的基础。代码如下:

import cv2
import numpy as np

# 读取你的裁切后图像
img = cv2.imread('cropped_digit_2.jpg')
# 转灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化(如果你的数字是黑色、背景白色,就把THRESH_BINARY改成THRESH_BINARY_INV)
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 提取图像中的所有轮廓
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 取面积最大的轮廓(就是数字2的轮廓)
digit_contour = max(contours, key=cv2.contourArea)

2. 识别轮廓上的端点

端点的特征很明显:它是轮廓的“尾巴”,3x3邻域内只有1个相邻的轮廓像素。我们遍历轮廓的每个点,找出符合条件的端点:

def find_endpoints(contour, binary_img):
    endpoints = []
    # 遍历轮廓的每个像素点
    for point in contour:
        x, y = point[0]
        # 统计3x3邻域内的轮廓像素(白色)数量
        neighbor_count = 0
        for dx in (-1, 0, 1):
            for dy in (-1, 0, 1):
                if dx == 0 and dy == 0:
                    continue  # 跳过自身
                # 确保坐标在图像范围内
                if 0 <= x+dx < binary_img.shape[1] and 0 <= y+dy < binary_img.shape[0]:
                    if binary_img[y+dy][x+dx] == 255:
                        neighbor_count += 1
        # 邻域内只有1个轮廓像素,说明是端点
        if neighbor_count == 1:
            endpoints.append((x, y))
    return endpoints

# 获取所有端点
endpoints = find_endpoints(digit_contour, binary)

💡 小提示:如果你的图像是黑字白底,记得把binary_img[y+dy][x+dx] == 255改成== 0。

3. 找出距离最近的一对端点

现在我们有了所有端点,接下来计算每一对端点的欧氏距离,找出距离最小的那一对:

def get_closest_endpoint_pair(endpoints):
    min_distance = float('inf')
    closest_pair = None
    # 遍历所有两两端点组合
    for i in range(len(endpoints)):
        for j in range(i + 1, len(endpoints)):
            p1 = np.array(endpoints[i])
            p2 = np.array(endpoints[j])
            # 计算欧氏距离
            distance = np.linalg.norm(p1 - p2)
            if distance < min_distance:
                min_distance = distance
                closest_pair = (p1, p2)
    return closest_pair

# 获取最近的端点对
closest_pair = get_closest_endpoint_pair(endpoints)

4. 连接端点完成修复

最后一步就是在原图上把这两个端点用线连起来,完成修复:

# 画一条白色的线(厚度可以自己调整,这里设为2)
cv2.line(img, tuple(closest_pair[0]), tuple(closest_pair[1]), (255, 255, 255), 2)
# 保存修复后的图像
cv2.imwrite('fixed_digit_2.jpg', img)
# 显示结果
cv2.imshow('Fixed Digit', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
新手友好的额外提示
  • 如果图像有噪声导致端点识别错误,可以在二值化前加一步模糊:gray = cv2.GaussianBlur(gray, (3, 3), 0)
  • 如果识别出的端点数量太多,可以调整邻域的大小(比如换成5x5),或者增加邻域内的像素数量阈值(比如把neighbor_count == 1改成neighbor_count <= 2)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:14:58