Opencv-solver/Sudoku_extractor.py
def crop_and_warp(self, img, crop_rect):
        """Crops and warps a rectangular section from an image into a square of similar size."""

        # Rectangle described by top left, top right, bottom right and bottom left points
        top_left, top_right, bottom_right, bottom_left = crop_rect[0], crop_rect[1], crop_rect[2], crop_rect[3]

        # Explicitly set the data type to float32 or `getPerspectiveTransform` will throw an error
        src = np.array([top_left, top_right, bottom_right, bottom_left], dtype='float32')

        # Get the longest side in the rectangle
        side = max([
            self.distance_between(bottom_right, top_right),
            self.distance_between(top_left, bottom_left),
            self.distance_between(bottom_right, bottom_left),
            self.distance_between(top_left, top_right)
        ])

        # Describe a square with side of the calculated length, this is the new perspective we want to warp to
        dst = np.array([[0, 0], [side - 1, 0], [side - 1, side - 1], [0, side - 1]], dtype='float32')

        # Gets the transformation matrix for skewing the image to fit a square by comparing the 4 before and after
        # points
        m = cv2.getPerspectiveTransform(src, dst)

        # Performs the transformation on the original image
        return cv2.warpPerspective(img, m, (int(side), int(side)))
Crops and warps a rectangular section from an image into a square of similar size.
Crops and warps a rectangular section from an image into a square of similar size.
Crops and warps a rectangular section from an image into a square of similar size .
def crop_and_warp(self, img, crop_rect):
        top_left, top_right, bottom_right, bottom_left = crop_rect[0], crop_rect[1], crop_rect[2], crop_rect[3]
        src = np.array([top_left, top_right, bottom_right, bottom_left], dtype='float32')
        side = max([
            self.distance_between(bottom_right, top_right),
            self.distance_between(top_left, bottom_left),
            self.distance_between(bottom_right, bottom_left),
            self.distance_between(top_left, top_right)
        ])
        dst = np.array([[0, 0], [side - 1, 0], [side - 1, side - 1], [0, side - 1]], dtype='float32')
        m = cv2.getPerspectiveTransform(src, dst)
        return cv2.warpPerspective(img, m, (int(side), int(side)))
def crop_and_warp ( self , img , crop_rect ) : top_left , top_right , bottom_right , bottom_left = crop_rect [ 0 ] , crop_rect [ 1 ] , crop_rect [ 2 ] , crop_rect [ 3 ] src = np . array ( [ top_left , top_right , bottom_right , bottom_left ] , dtype = 'float32' ) side = max ( [ self . distance_between ( bottom_right , top_right ) , self . distance_between ( top_left , bottom_left ) , self . distance_between ( bottom_right , bottom_left ) , self . distance_between ( top_left , top_right ) ] ) dst = np . array ( [ [ 0 , 0 ] , [ side - 1 , 0 ] , [ side - 1 , side - 1 ] , [ 0 , side - 1 ] ] , dtype = 'float32' ) m = cv2 . getPerspectiveTransform ( src , dst ) return cv2 . warpPerspective ( img , m , ( int ( side ) , int ( side ) ) )
Crops and warps a rectangular section from an image into a square of similar size.
Crops and warps a rectangular section from an image into a square of similar size .
"""Crops and warps a rectangular section from an image into a square of similar size.""" # Rectangle described by top left, top right, bottom right and bottom left points # Explicitly set the data type to float32 or `getPerspectiveTransform` will throw an error # Get the longest side in the rectangle # Describe a square with side of the calculated length, this is the new perspective we want to warp to # Gets the transformation matrix for skewing the image to fit a square by comparing the 4 before and after # points # Performs the transformation on the original image
self
img
crop_rect