其他
教程 | OpenCV场景文字检测
星标或者置顶【OpenCV学堂】
干货文章与技术教程第一时间送达
TEXT扩展模块概述
OpenCV在TEXT扩展模块中支持场景文字识别,最早的场景文字检测是基于级联检测器实现,OpenCV中早期的场景文字检测是基于极值区域文本定位与识别、最新的OpenCV3.4.x之后的版本添加了卷积神经网络实现场景文字检测,后者的准确性与稳定性比前者有了很大的改观,不再是鸡肋算法,是可以应用到实际场景中的。值得一提的是基于CNN实现场景文字检测算法OpenCV中采用了是华中科技大学贡献的模型,模型结构如下:
代码演示
基于极值区域文本定位的方法实现场景文字检测演示如下:
def cascade_classfier_text_detect():
img = cv.imread("D:/images/cover_01.jpg")
vis = img.copy()
# Extract channels to be processed individually
channels = cv.text.computeNMChannels(img)
cn = len(channels)-1
for c in range(0,cn):
channels.append((255-channels[c]))
# Apply the default cascade classifier to each independent channel (could be done in parallel)
print("Extracting Class Specific Extremal Regions from "+str(len(channels))+" channels ...")
print(" (...) this may take a while (...)")
for channel in channels:
erc1 = cv.text.loadClassifierNM1('trained_classifierNM1.xml')
er1 = cv.text.createERFilterNM1(erc1,16,0.00015,0.13,0.2,True,0.1)
erc2 = cv.text.loadClassifierNM2('trained_classifierNM2.xml')
er2 = cv.text.createERFilterNM2(erc2,0.5)
regions = cv.text.detectRegions(channel,er1,er2)
rects = cv.text.erGrouping(img,channel,[r.tolist() for r in regions])
#Visualization
for r in range(0,np.shape(rects)[0]):
rect = rects[r]
cv.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (255, 0, 0), 2)
cv.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 0, 255), 1)
#Visualization
cv.imshow("Text detection result", vis)
cv.imwrite("D:/test_detection_demo_02.png", vis)
cv.waitKey(0)
基于卷积神经网络模型实现场景文字检测演示如下:
def cnn_text_detect():
image = cv.imread("D:/images/cover_01.jpg")
cv.imshow("input", image)
result = image.copy()
detector = cv.text.TextDetectorCNN_create("textbox.prototxt", "TextBoxes_icdar13.caffemodel")
boxes, scores = detector.detect(image);
threshold = 0.5
for r in range(np.shape(boxes)[0]):
if scores[r] > threshold:
rect = boxes[r]
cv.rectangle(result, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (255, 0, 0), 2)
cv.imshow("Text detection result", result)
cv.waitKey()
cv.waitKey(0)
cv.destroyAllWindows()
运行结果
基于极值区域文本定位
基于卷积神经网络检测
对比发现,明显基于卷积神经网络的方法更加的靠谱!所以请使用TEXT模块中的卷积神经网络实现场景文字检测。
合抱之木,生于毫末
九层之台,起于累土
千里之行,始于足下
欢迎扫码加入【OpenCV研习社】
听说点【好看】会有好运来