Created
October 30, 2016 03:17
-
-
Save hmmbug/7f997ddfae5b33ea4264d287ba9a4894 to your computer and use it in GitHub Desktop.
Extracting text with OpenCV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A python port of http://stackoverflow.com/a/23565051/2416742 | |
import cv2 | |
def detect_letters(img, struct_elem_x=17, struct_elem_y=3, contour_size=100): | |
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
img_sobel = cv2.Sobel(img_gray, cv2.CV_8U, 1, 0, ksize=3, scale=1, | |
delta=0, borderType=cv2.BORDER_DEFAULT) | |
ret, img_threshold = cv2.threshold(img_sobel, 0, 255, | |
cv2.THRESH_OTSU + cv2.THRESH_BINARY) | |
element = cv2.getStructuringElement(cv2.MORPH_RECT, | |
(struct_elem_x, struct_elem_y)) | |
img_morphex = cv2.morphologyEx(img_threshold, cv2.MORPH_CLOSE, element) | |
img_contours, contours, hierarchy = cv2.findContours(img_morphex, 0, 1) | |
contours_poly = [] | |
for c in contours: | |
if len(c) < contour_size: | |
continue | |
poly = cv2.approxPolyDP(c, epsilon=3, closed=True) | |
x, y, w, h = cv2.boundingRect(poly) | |
if w > h: | |
contours_poly.append((x, y, w, h)) | |
return contours_poly | |
if __name__ == '__main__': | |
# load image & find text | |
img = cv2.imread('my_image.png', cv2.IMREAD_COLOR) | |
res = detect_letters(img, struct_elem_x=30, struct_elem_y=30, contour_size=100) | |
# draw results | |
for r in res: | |
x, y, w, h = r | |
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) | |
cv2.imshow('image', img) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment