Opencv-solver/Sudoku_extractor.py
def find_corners_of_largest_polygon(img):
        """Finds the 4 extreme corners of the largest contour in the image."""
        contours, h = cv2.findContours(img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # Find contours
        contours = sorted(contours, key=cv2.contourArea, reverse=True)  # Sort by area, descending
        polygon = contours[0]  # Largest image

        # Use of `operator.itemgetter` with `max` and `min` allows us to get the index of the point Each point is an
        # array of 1 coordinate, hence the [0] getter, then [0] or [1] used to get x and y respectively.

        # Bottom-right point has the largest (x + y) value
        # Top-left has point smallest (x + y) value
        # Bottom-left point has smallest (x - y) value
        # Top-right point has largest (x - y) value
        bottom_right, _ = max(enumerate([pt[0][0] + pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
        top_left, _ = min(enumerate([pt[0][0] + pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
        bottom_left, _ = min(enumerate([pt[0][0] - pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
        top_right, _ = max(enumerate([pt[0][0] - pt[0][1] for pt in polygon]), key=operator.itemgetter(1))

        # Return an array of all 4 points using the indices
        # Each point is in its own array of one coordinate
        return [polygon[top_left][0], polygon[top_right][0], polygon[bottom_right][0], polygon[bottom_left][0]]
Finds the 4 extreme corners of the largest contour in the image.
Finds the 4 extreme corners of the largest contour in the image.
Finds the 4 extreme corners of the largest contour in the image .
def find_corners_of_largest_polygon(img):
        contours, h = cv2.findContours(img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  
        contours = sorted(contours, key=cv2.contourArea, reverse=True)  
        polygon = contours[0]  
        bottom_right, _ = max(enumerate([pt[0][0] + pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
        top_left, _ = min(enumerate([pt[0][0] + pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
        bottom_left, _ = min(enumerate([pt[0][0] - pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
        top_right, _ = max(enumerate([pt[0][0] - pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
        return [polygon[top_left][0], polygon[top_right][0], polygon[bottom_right][0], polygon[bottom_left][0]]
def find_corners_of_largest_polygon ( img ) : contours , h = cv2 . findContours ( img . copy ( ) , cv2 . RETR_EXTERNAL , cv2 . CHAIN_APPROX_SIMPLE ) contours = sorted ( contours , key = cv2 . contourArea , reverse = True ) polygon = contours [ 0 ] bottom_right , _ = max ( enumerate ( [ pt [ 0 ] [ 0 ] + pt [ 0 ] [ 1 ] for pt in polygon ] ) , key = operator . itemgetter ( 1 ) ) top_left , _ = min ( enumerate ( [ pt [ 0 ] [ 0 ] + pt [ 0 ] [ 1 ] for pt in polygon ] ) , key = operator . itemgetter ( 1 ) ) bottom_left , _ = min ( enumerate ( [ pt [ 0 ] [ 0 ] - pt [ 0 ] [ 1 ] for pt in polygon ] ) , key = operator . itemgetter ( 1 ) ) top_right , _ = max ( enumerate ( [ pt [ 0 ] [ 0 ] - pt [ 0 ] [ 1 ] for pt in polygon ] ) , key = operator . itemgetter ( 1 ) ) return [ polygon [ top_left ] [ 0 ] , polygon [ top_right ] [ 0 ] , polygon [ bottom_right ] [ 0 ] , polygon [ bottom_left ] [ 0 ] ]
Finds the 4 extreme corners of the largest contour in the image.
Finds the 4 extreme corners of the largest contour in the image .
"""Finds the 4 extreme corners of the largest contour in the image.""" # Find contours # Sort by area, descending # Largest image # Use of `operator.itemgetter` with `max` and `min` allows us to get the index of the point Each point is an # array of 1 coordinate, hence the [0] getter, then [0] or [1] used to get x and y respectively. # Bottom-right point has the largest (x + y) value # Top-left has point smallest (x + y) value # Bottom-left point has smallest (x - y) value # Top-right point has largest (x - y) value # Return an array of all 4 points using the indices # Each point is in its own array of one coordinate
img