Opencv-solver/Sudoku_extractor.py
def find_largest_feature(self, inp_img, scan_tl=None, scan_br=None, x_idx=None, y_idx=None):
        """
        Uses the fact the `floodFill` function returns a bounding box of the area it filled to find the biggest
        connected pixel structure in the image. Fills this structure in white, reducing the rest to black.
        """
        img = inp_img.copy()  # Copy the image, leaving the original untouched
        height, width = img.shape[:2]

        max_area = 0
        seed_point = (None, None)

        if scan_tl is None:
            scan_tl = [0, 0]

        if scan_br is None:
            scan_br = [width, height]

        # Loop through the image
        for x in range(scan_tl[0], scan_br[0]):
            for y in range(scan_tl[1], scan_br[1]):
                # Only operate on light or white squares
                if img.item(y,
                            x) == 255 and x < width and y < height:  # Note that .item() appears to take input as y, x

                    area = cv2.floodFill(img, None, (x, y), 64)
                    if area[0] > max_area:  # Gets the maximum bound area which should be the grid
                        max_area = area[0]
                        seed_point = (x, y)

        # Colour everything grey (compensates for features outside of our middle scanning range
        for x in range(width):
            for y in range(height):
                if img.item(y, x) == 255 and x < width and y < height:
                    cv2.floodFill(img, None, (x, y), 0)

        # Highlight the main feature
        if all([p is not None for p in seed_point]):
            mask = np.zeros((height + 2, width + 2), np.uint8)  # Mask that is 2 pixels bigger than the image
            cv2.floodFill(img, mask, seed_point, 255)

        # ---------------------------------------------------------------------------------
        num = pytesseract.image_to_string(img, lang='eng',
                                        config='--psm 10 --oem 1 -c tessedit_char_whitelist=0123456789')
        num = int(num[0]) if num[0] != "\x0c" else 0
        self.board[y_idx, x_idx] = num
        # plt.imshow(img)
        # plt.show()
        # ---------------------------------------------------------------------------------

        Uses the fact the `floodFill` function returns a bounding box of the area it filled to find the biggest
        connected pixel structure in the image. Fills this structure in white, reducing the rest to black.
        
Uses the fact the `floodFill` function returns a bounding box of the area it filled to find the biggest
connected pixel structure in the image. Fills this structure in white, reducing the rest to black.
Uses the fact the ` floodFill ` function returns a bounding box of the area it filled to find the biggest connected pixel structure in the image . Fills this structure in white reducing the rest to black .
def find_largest_feature(self, inp_img, scan_tl=None, scan_br=None, x_idx=None, y_idx=None):
        img = inp_img.copy()  
        height, width = img.shape[:2]
        max_area = 0
        seed_point = (None, None)
        if scan_tl is None:
            scan_tl = [0, 0]
        if scan_br is None:
            scan_br = [width, height]
        for x in range(scan_tl[0], scan_br[0]):
            for y in range(scan_tl[1], scan_br[1]):
                if img.item(y,
                            x) == 255 and x < width and y < height:  
                    area = cv2.floodFill(img, None, (x, y), 64)
                    if area[0] > max_area:  
                        max_area = area[0]
                        seed_point = (x, y)
        for x in range(width):
            for y in range(height):
                if img.item(y, x) == 255 and x < width and y < height:
                    cv2.floodFill(img, None, (x, y), 0)
        if all([p is not None for p in seed_point]):
            mask = np.zeros((height + 2, width + 2), np.uint8)  
            cv2.floodFill(img, mask, seed_point, 255)
        num = pytesseract.image_to_string(img, lang='eng',
                                        config='--psm 10 --oem 1 -c tessedit_char_whitelist=0123456789')
        num = int(num[0]) if num[0] != "\x0c" else 0
        self.board[y_idx, x_idx] = num
def find_largest_feature ( self , inp_img , scan_tl = None , scan_br = None , x_idx = None , y_idx = None ) : img = inp_img . copy ( ) height , width = img . shape [ : 2 ] max_area = 0 seed_point = ( None , None ) if scan_tl is None : scan_tl = [ 0 , 0 ] if scan_br is None : scan_br = [ width , height ] for x in range ( scan_tl [ 0 ] , scan_br [ 0 ] ) : for y in range ( scan_tl [ 1 ] , scan_br [ 1 ] ) : if img . item ( y , x ) == 255 and x < width and y < height : area = cv2 . floodFill ( img , None , ( x , y ) , 64 ) if area [ 0 ] > max_area : max_area = area [ 0 ] seed_point = ( x , y ) for x in range ( width ) : for y in range ( height ) : if img . item ( y , x ) == 255 and x < width and y < height : cv2 . floodFill ( img , None , ( x , y ) , 0 ) if all ( [ p is not None for p in seed_point ] ) : mask = np . zeros ( ( height + 2 , width + 2 ) , np . uint8 ) cv2 . floodFill ( img , mask , seed_point , 255 ) num = pytesseract . image_to_string ( img , lang = 'eng' , config = '--psm 10 --oem 1 -c tessedit_char_whitelist=0123456789' ) num = int ( num [ 0 ] ) if num [ 0 ] != "\x0c" else 0 self . board [ y_idx , x_idx ] = num
Uses the fact the `floodFill` function returns a bounding box of the area it filled to find the biggest
connected pixel structure in the image.
Uses the fact the ` floodFill ` function returns a bounding box of the area it filled to find the biggest connected pixel structure in the image .
"""
        Uses the fact the `floodFill` function returns a bounding box of the area it filled to find the biggest
        connected pixel structure in the image. Fills this structure in white, reducing the rest to black.
        """ # Copy the image, leaving the original untouched # Loop through the image # Only operate on light or white squares # Note that .item() appears to take input as y, x # Gets the maximum bound area which should be the grid # Colour everything grey (compensates for features outside of our middle scanning range # Highlight the main feature # Mask that is 2 pixels bigger than the image # --------------------------------------------------------------------------------- # plt.imshow(img) # plt.show() # ---------------------------------------------------------------------------------
self
inp_img
scan_tl
scan_br
x_idx
y_idx