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
| #!/usr/bin/env python | |
| '''Crop an image to just the portions containing text. | |
| Usage: | |
| ./crop_morphology.py path/to/image.jpg | |
| This will place the cropped image in path/to/image.crop.png. | |
| For details on the methodology, see | |
| http://www.danvk.org/2015/01/07/finding-blocks-of-text-in-an-image-using-python-opencv-and-numpy.html | |
| UPDATE 2017-05-07 by hmmbug: | |
| - Removed dependency on PIL and |
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, |