在扫描/拍摄的医疗文档中删除网格

2024-09-26 18:05:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名牙医专业的学生,目前正在尝试编写一个脚本,用于分析和提取牙科记录中的手写数字。我已经完成了一个粗略的脚本版本,但是我的识别率很低。分析数据的一个大问题是网格很难删除。你知道吗

我要分析的扫描表单(白色字段表示匿名):

空窗体:

对于这个问题,我尝试了不同的解决方案(腐蚀/膨胀、HoughLineTransform和线的吸引子)。 在空模板中使用featurematching和Subtracting目前可以得到最好的结果。你知道吗

结果:

腐蚀和扩大这幅图像会得到更好的结果

结果:
![][4]

但这几乎每次我尝试都需要一个新的校准。 你知道我的问题有什么更优雅的解决办法吗。 冲浪匹配能提供更好的结果吗? 非常感谢你!你知道吗

以下是我目前的代码:

GOOD_MATCH_PERCENT = 0.15

def match_img_to_template(input_img, template_img, MAX_FEATURES, GOOD_MATCH_PERCENT):

    # blurring of the input image
    template_img = cv2.GaussianBlur(template_img, (3, 3), cv2.BORDER_DEFAULT)

    # equalizing the histogramm of the input image
    img_preprocessed = cv2.equalizeHist(input_img)

    # ORB Detector
    orb = cv2.ORB_create(MAX_FEATURES)
    kp1, des1 = orb.detectAndCompute(img_preprocessed, None)
    kp2, des2 = orb.detectAndCompute(template_img, None)

    # Brute Force Matching
    matcher= cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
    matches = matcher.match(des1, des2, None)

    matches.sort(key=lambda x:x.distance, reverse=False)

    numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
    matches = matches[:numGoodMatches]

    # Remove not so good matches
    numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
    matches = matches[:numGoodMatches]

    # Extract location of good matches
    points1 = np.zeros((len(matches), 2), dtype=np.float32)
    points2 = np.zeros((len(matches), 2), dtype=np.float32)

    for i, match in enumerate(matches):
        points1[i, :] = kp1[match.queryIdx].pt
        points2[i, :] = kp2[match.trainIdx].pt

    # Find homography
    h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)

    # Use homography
    height, width = template_img.shape
    input_warped = cv2.warpPerspective(input_img, h, (width, height))

    ret1, input_warped_thresh = cv2.threshold(input_warped,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

    diff = cv2.absdiff(template_img, input_warped_thresh)

    ret, diff = cv2.threshold(diff, 20, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C + cv2.THRESH_BINARY)

    diff = cv2.equalizeHist(diff)

    # Create kernels
    kernel1 = np.ones((3,3),np.uint8)
    kernel2 = np.ones((6,6), np.uint8)
    # erode dilate to remove the grid
    diff_erode = cv2.erode(diff,kernel1)
    diff_dilated = cv2.dilate(diff_erode,kernel2)
    # invert diff_dilate
    diff_dilated_inv = cv2.bitwise_not(diff_dilated)

    return diff_dilated_inv

Tags: theimginputlenmatchnpdifftemplate

热门问题