mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-19 02:16:34 +08:00
61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
#!/usr/bin/python
|
|
|
|
import sys
|
|
import os
|
|
|
|
import cv2
|
|
import numpy as np
|
|
from matplotlib import pyplot as plt
|
|
|
|
print('\ntextdetection.py')
|
|
print(' A demo script of the Extremal Region Filter algorithm described in:')
|
|
print(' Neumann L., Matas J.: Real-Time Scene Text Localization and Recognition, CVPR 2012\n')
|
|
|
|
|
|
if (len(sys.argv) < 2):
|
|
print(' (ERROR) You must call this script with an argument (path_to_image_to_be_processed)\n')
|
|
quit()
|
|
|
|
pathname = os.path.dirname(sys.argv[0])
|
|
|
|
|
|
img = cv2.imread(str(sys.argv[1]))
|
|
# for visualization
|
|
vis = img.copy()
|
|
|
|
|
|
# Extract channels to be processed individually
|
|
channels = cv2.text.computeNMChannels(img)
|
|
# Append negative channels to detect ER- (bright regions over dark background)
|
|
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 = cv2.text.loadClassifierNM1(pathname+'/trained_classifierNM1.xml')
|
|
er1 = cv2.text.createERFilterNM1(erc1,16,0.00015,0.13,0.2,True,0.1)
|
|
|
|
erc2 = cv2.text.loadClassifierNM2(pathname+'/trained_classifierNM2.xml')
|
|
er2 = cv2.text.createERFilterNM2(erc2,0.5)
|
|
|
|
regions = cv2.text.detectRegions(channel,er1,er2)
|
|
|
|
rects = cv2.text.erGrouping(img,channel,[r.tolist() for r in regions])
|
|
#rects = cv2.text.erGrouping(img,gray,[x.tolist() for x in regions], cv2.text.ERGROUPING_ORIENTATION_ANY,'../../GSoC2014/opencv_contrib/modules/text/samples/trained_classifier_erGrouping.xml',0.5)
|
|
|
|
#Visualization
|
|
for r in range(0,np.shape(rects)[0]):
|
|
rect = rects[r]
|
|
cv2.rectangle(vis, (rect[0],rect[1]), (rect[0]+rect[2],rect[1]+rect[3]), (0, 255, 255), 2)
|
|
|
|
|
|
#Visualization
|
|
vis = vis[:,:,::-1] #flip the colors dimension from BGR to RGB
|
|
plt.imshow(vis)
|
|
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
|
|
plt.show()
|