如题,在keras识别的时候发现直接用python统计像素垂直分割与opencv-python的canny检测分割上速度差异很大,
算上预处理时间,垂直分割大概只有8帧/s, 而canny检测后findcontour却有21帧/s,
这样就不是很清楚究竟是哪里影响到了速度,难道opencv ,c++真就恐怖如斯?
分割+识别一帧时间如下,识别模型都是一样的,图像处理只是各种取黑白阈值加高斯去噪
Canny time is: 0.0072460174560546875 (cv2.canny + cv2.findcontour)
vertical time is: 0.04143167495727539
问题是:1、同样是遍历像素点进行分割,垂直分割遍历次数似乎还少一些,引起差异的原因是什么,是否可以通过换掉cpython,jit解释器以及for循环嵌套、添加静态类型变量的改进等方法解决速度差异,具体如何改进
2、canny边缘检测算法:canny检测发现形态学处理用(1,11)的kernel可以去除字体的断裂,但是经过开闭运算和高斯去噪仍然会有少量噪点残余,只要出现一个噪点后,经形态学处理后导致噪点直接变成竖直直线,因此噪点比较难跟图像小数点区分开来,目前在用宽度阈值但不是特别有效,因此调阈值特别困难,有什么好的建议吗
代码如下:
在定位并获取清晰的黑白值图像bitwise之后输入这个函数,rec_x1,rec_y1则是bitwise左上角坐标,a是分割阈值,thresh仅用于获得shape
def find_end(start, arg, black, white, width, black_max, white_max, a):
end = start + 1
for m in range(start + 1, width - 1):
if (black[m] if arg else white[m]) > (a * black_max if arg else a * white_max):
end = m
break
return end
def vertical_crop(thresh, bitwise, rec_x1, rec_y1, a=0.98, min_width=8):
num = []
flag2 = 0
soft = []
dt_boxes = []
vertical = thresh.copy()
white = []
black = []
height = vertical.shape[0] # 263
width = vertical.shape[1] # 400
# print('height',height)
# print('width',width)
white_max = 0
black_max = 0
# 计算每一列的黑白像素总和
for i in range(width):
line_white = 0
line_black = 0
for j in range(height):
if vertical[j][i] == 255:
line_white += 1
if vertical[j][i] == 0:
line_black += 1
white_max = max(white_max, line_white)
black_max = max(black_max, line_black)
white.append(line_white)
black.append(line_black)
# print('white', white)
# print('black', black)
# arg为true表示黑底白字,False为白底黑字
arg = True
if black_max < white_max:
arg = False
n = 1
start = 1
end = 2
s_width = 28
s_height = 28
while n < width - 2:
n += 1
# 判断是白底黑字还是黑底白字 0.05参数对应上面的0.95 可作调整
b = 1 - a
if (white[n] if arg else black[n]) > (b * white_max if arg else b * black_max):
start = n
end = find_end(start, arg, black, white, width, black_max, white_max, a)
n = end
if end - start > min_width:
# cj = vertical[1:height, start:end]
cv2.rectangle(vertical, (start, 1), (end, height), (0, 0, 0), 1)
crop = bitwise[1:height, start:end]
crop = reformat(crop)
number, softmax = recognize(crop)
if softmax <= 0.95 or (end-start)/height >= 1.5:
flag2 = -1
else:
pass
num.append(number)
soft.append(softmax)
boxes = [rec_x1 + start, rec_y1 + 1, rec_x1 + end, rec_y1 + height - 1]
dt_boxes.append(boxes)
cv2.imshow('corp', vertical)
return num, soft, vertical, dt_boxes, flag2
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。