diff --git a/camelot/image_processing.py b/camelot/image_processing.py index 7b871011..1b5d962a 100644 --- a/camelot/image_processing.py +++ b/camelot/image_processing.py @@ -8,7 +8,6 @@ def adaptive_threshold(imagename, process_background=False, blocksize=15, c=-2): """Thresholds an image using OpenCV's adaptiveThreshold. - Parameters ---------- imagename : string @@ -18,41 +17,29 @@ def adaptive_threshold(imagename, process_background=False, blocksize=15, c=-2): blocksize : int, optional (default: 15) Size of a pixel neighborhood that is used to calculate a threshold value for the pixel: 3, 5, 7, and so on. - For more information, refer `OpenCV's adaptiveThreshold `_. c : int, optional (default: -2) Constant subtracted from the mean or weighted mean. Normally, it is positive but may be zero or negative as well. - For more information, refer `OpenCV's adaptiveThreshold `_. - Returns ------- img : object numpy.ndarray representing the original image. threshold : object numpy.ndarray representing the thresholded image. - """ img = cv2.imread(imagename) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) - - if process_background: - threshold = cv2.adaptiveThreshold( - gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blocksize, c - ) - else: - threshold = cv2.adaptiveThreshold( - np.invert(gray), - 255, - cv2.ADAPTIVE_THRESH_GAUSSIAN_C, - cv2.THRESH_BINARY, - blocksize, - c, - ) + if not process_background: + gray = np.invert(gray) + threshold = cv2.adaptiveThreshold( + gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, blocksize, c + ) return img, threshold + def find_lines( threshold, regions=None, direction="horizontal", line_scale=15, iterations=0 ): @@ -177,7 +164,6 @@ def find_contours(vertical, horizontal): def find_joints(contours, vertical, horizontal): """Finds joints/intersections present inside each table boundary. - Parameters ---------- contours : list @@ -188,7 +174,6 @@ def find_joints(contours, vertical, horizontal): numpy.ndarray representing pixels where vertical lines lie. horizontal : object numpy.ndarray representing pixels where horizontal lines lie. - Returns ------- tables : dict @@ -196,22 +181,12 @@ def find_joints(contours, vertical, horizontal): in that boundary as their value. Keys are of the form (x1, y1, x2, y2) where (x1, y1) -> lb and (x2, y2) -> rt in image coordinate space. - """ - joints = np.multiply(vertical, horizontal) tables = {} for c in contours: x, y, w, h = c - roi = joints[y : y + h, x : x + w] - try: - __, jc, __ = cv2.findContours( - roi.astype(np.uint8), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE - ) - except ValueError: - # for opencv backward compatibility - jc, __ = cv2.findContours( - roi.astype(np.uint8), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE - ) + roi = joints[y:y+h, x:x+w] + jc = cv2.findContours(roi.astype(np.uint8), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)[1] if len(jc) <= 4: # remove contours with less than 4 joints continue joint_coords = []