去除历史扫描文件图片中的折痕,主要为技术探讨。
参考文献:http://www.doc88.com/p-0886436885083.html
主要使用:二值化与骨架提取
文章基于路径搜索折痕检测方法的系统框图如下:
首先,包含折痕的感兴 趣区域可以根据亮度值的分布提取出来。基于凸包络的算法用于提取出感兴趣区 域的阴影图像。利用折痕两侧的亮度不同对阴影图像进行滤波得到折痕位置图。 对滤波后的图像进行二值化。保留最大连通域。之后运用形态学膨胀和骨骼细化操作产生路径搜索图。最后运用迪杰斯特拉算法进行路径搜索的到折痕的准确位置。
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
待续
总结与说明:
以上为技术探讨,如用于生产需要用C++重写或者Cython加速。