import numpy as np
import cv2
import sys
import matplotlib.pyplot as plt
img = cv2.imread(sys.argv[1],0)
h,w = img.shape[:2]
img = cv2.resize(img,(h//5,w//5),interpolation=cv2.INTER_CUBIC)
_,bin = cv2.threshold(img,128,255,cv2.THRESH_BINARY)
horizontal_sum = np.sum(bin, axis=1)
plt.plot(horizontal_sum, range(horizontal_sum.shape[0]))
plt.gca().invert_yaxis()
plt.show()
源码如下:
#coding:utf-8
'''
表格生成线条坐标
'''
import cv2
import numpy as np
from matplotlib import pyplot as plt
import json
import sys
import subprocess
import os
class detectTable(object):
def __init__(self, src_img):
self.src_img = src_img
def run(self):
if len(self.src_img.shape) == 2: # 灰度图
gray_img = self.src_img
elif len(self.src_img.shape) ==3:
gray_img = cv2.cvtColor(self.src_img, cv2.COLOR_BGR2GRAY)
thresh_img = cv2.adaptiveThreshold(~gray_img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,-2)
h_img = thresh_img.copy()
v_img = thresh_img.copy()
scale = 15
h_size = int(h_img.shape[1]/scale)
h_structure = cv2.getStructuringElement(cv2.MORPH_RECT,(h_size,1)) # 形态学因子
h_erode_img = cv2.erode(h_img,h_structure,1)
h_dilate_img = cv2.dilate(h_erode_img,h_structure,1)
# cv2.imshow("h_erode",h_dilate_img)
v_size = int(v_img.shape[0] / scale)
v_structure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, v_size)) # 形态学因子
v_erode_img = cv2.erode(v_img, v_structure, 1)
v_dilate_img = cv2.dilate(v_erode_img, v_structure, 1)
mask_img = h_dilate_img+v_dilate_img
joints_img = cv2.bitwise_and(h_dilate_img,v_dilate_img)
joints_img = cv2.dilate(joints_img,None,iterations=3)
cv2.imwrite("joints.png",~joints_img)
cv2.imwrite("mask.png",~mask_img)
if __name__=='__main__':
img = cv2.imread(sys.argv[1])
detectTable(img).run()
基于惯性大水滴滴水算法和支持向量机的粘连字符验证码识别
代码地址:https://download.youkuaiyun.com/download/yinchuandong2/8062831
论文地址:https://download.youkuaiyun.com/download/yinchuandong2/8062837
博客地址:https://blog.youkuaiyun.com/yinchuandong2/article/details/40340735