def _find_largest_square(self):

        contours, image = self._get_major_contours(self.image)

        # Store contours that could be the puzzle using the contour's area
        # as the key.
        possible_puzzles = {}

        for contour in contours:
            contour_length = cv2.arcLength(contour, True)
            area = cv2.contourArea(contour)

            # Approximate the contour to a polygon.
            contour = cv2.approxPolyDP(contour, 0.02 * contour_length, True)

            # Find contours with 4 vertices and an area greater than a
            # third of the image area with a convex shape.
            if len(contour) == 4 and (
                    area > image.size / 3.0 and cv2.isContourConvex(contour)):

                # Find the largest cosine of the angles in the contour.
                contour_reshaped = contour.reshape(-1, 2)
                max_cos = np.max([self._angle_cos(
                        contour_reshaped[i],
                        contour_reshaped[(i+1) % 4],
                        contour_reshaped[(i+2) % 4]) for i in xrange(4)])

                # If the max cosine is almost zero (a square),
                # it is most likely the Sudoku puzzle.
                if max_cos < 0.1:
                    possible_puzzles[area] = contour

        # We get the smallest contour because sometimes interference around the
        # edge of the image creates a contour almost the size of the image,
        # and we don't want to use that contour.
        areas = possible_puzzles.keys()
        areas.sort()
        return possible_puzzles[areas[0]]
Find the largest square in the image, most likely the puzzle.
dataset/ETHPy150Open GoogleCloudPlatform/appengine-opencv-sudoku-python/sudoku_image_parser.py/SudokuImageParser._find_largest_square