图像折痕与扭曲去除

一、前言

去除历史扫描文件图片中的折痕,主要为技术探讨。
参考文献:http://www.doc88.com/p-0886436885083.html
主要使用:二值化与骨架提取

文章基于路径搜索折痕检测方法的系统框图如下:

首先,包含折痕的感兴 趣区域可以根据亮度值的分布提取出来。基于凸包络的算法用于提取出感兴趣区 域的阴影图像。利用折痕两侧的亮度不同对阴影图像进行滤波得到折痕位置图。 对滤波后的图像进行二值化。保留最大连通域。之后运用形态学膨胀和骨骼细化操作产生路径搜索图。最后运用迪杰斯特拉算法进行路径搜索的到折痕的准确位置。
在这里插入图片描述

二、统计直方图获取ROI

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

def calcAndDrawHist(image):  
    return cv2.calcHist([image], [0], None, [256], [0.0,255.0])


def show_hist(original_img):
    b, g, r = cv2.split(original_img)
    histImgB = calcAndDrawHist(b)  
    histImgG = calcAndDrawHist(g)  
    histImgR = calcAndDrawHist(r)  
    plt.plot(histImgB,'b')
    plt.plot(histImgG,'g')
    plt.plot(histImgR,'r')
    plt.show()
    
def count_colors(original_img): # 测试用
    height,width = original_img.shape[:2]
    b, g, r = cv2.split(original_img)
    b_color = np.multiply(b,65025)
    g_color = np.multiply(g,250)
    colors = b_color + g_color + r
    num_color = colors.reshape(1,-1)[0]
    len_color = int(len(num_color)*(1-0.05))
    print("[num_color]",len(num_color))
    print("[len_color]",len_color)

    histImg = np.bincount(num_color)
    New_Img = np.zeros((height,width),dtype=np.uint8)
    for y in range(height):
        for x in range(width):
            if 55955  < colors[y,x]:
                New_Img[y,x] = 255
    plt.plot(histImg,'b')
    plt.show()

    
def count_gray(original_img):
    height,width = original_img.shape[:2]
    gray_img = cv2.cvtColor(original_img,cv2.COLOR_BGR2GRAY)
    num_color = gray_img.reshape(1,-1)[0]
    len_color = int(len(num_color)*(1-0.05))
    print("[num_color]",len(num_color))
    print("[len_color]",len_color)

    New_Img = np.zeros((height,width),dtype=np.uint8)
    for y in range(height):
        for x in range(width):
            if 190  < gray_img[y,x]:
                New_Img[y,x] = 255
    cv2.imwrite("./count_gray.png",New_Img)
    return New_Img


    
def draw_convexHull(original_img,New_Img):
    height,width = original_img.shape[:2]
    ret, thresh = cv2.threshold(New_Img, 127, 255, cv2.THRESH_BINARY)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    contours_ok = []
    len_contours = len(contours)
    if 0 < len_contours:
        contours_temp = []
        for c in contours:
            x, y, w, h = cv2.boundingRect(c)
            if w >= 5 and h >= 3:
                contours_temp.append([x, y, x+w, y+h])
        if 0 < len(contours_temp):
            contours_np = np.array(contours_temp)
            col_max = np.max(contours_np,axis=0)
            col_min = np.min(contours_np,axis=0)
            contours_ok = [col_min[0],col_min[1],col_max[2],col_max[3]]
            print("contours_ok",contours_ok)
            cv2.rectangle(original_img, (0,contours_ok[1]+3), (width,contours_ok[3]+3), (255,255,255), 4)
            cv2.imshow('line', original_img)
            cv2.imwrite("./ROI.png",original_img)
            cv2.waitKey(0)
            cv2.destroyAllWindows()

if __name__ == '__main__':  
    original_img = cv2.imread("./2222.jpg")
    show_hist(original_img)
    count_colors(original_img)
    New_Img = count_gray(original_img)
    draw_convexHull(original_img,New_Img)

在这里插入图片描述
如下为原图(original_img)与处理后的二值图(count_gray.png)以及ROI
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

文档布局分析 (Document Layout Analysis) 是识别和分类文本文档的扫描图像中的感兴趣区域(RoI, Regions of Interest) 的过程。

在对扫描文档进行OCR信息提取时,发现扫描文档卷曲折行一系列问题导致OCR提取效果不是很好。想起对文本分析和定义文档的逻辑结构。例如文本块、段落、行的位置;是否有应该重建的表格;是否有“图像”“条形码等”。

文档布局分析是几何和逻辑标签的结合。它通常在将文档图像发送到OCR引擎之前执行,但也可用于检测大型存档中同一文档的重复副本,或者通过其结构或图示内容索引文档。

关于文档布局分析 & 扭曲文档图像恢复:【原文请点击】【国际会议录丛书(ICPS】
通过形态学操作提取水平线和垂直线:【示例代码】【表格提取示例代码】

关于算法流程及效果演示请见原文。

开源代码如下:
https://github.com/chadoliver/cosc428-structor 复现了docstrum【1993年,O’ Gorman 在TPAMI中发表】
https://github.com/chulwoopack/docstrum   对前一个开源代码进行了优化
https://github.com/mzucker/page_dewarp   CTM 方法非官方实现【CBDAR 2007论文】
https://github.com/phulin/rebook       [论文 TPAMI 2012]
https://github.com/ybur-yug/python_ocr_tutorial Leptonica库
https://github.com/scantailor/scantailor    scantailor 古老的将拍照的书页自动转换为无卷曲的扫描书页,【使用或构建方法】

总结与说明:
以上为技术探讨,如用于生产需要用C++重写或者Cython加速。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongpingWang

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值