Ivan Iziumov
Ivan Iziumov

Reputation: 35

Part of an image into numpy array

I'm working on a project about real time sudoku recognition and I ran into a problem. I want to take a part of an image, let's say a part of a sudoku thats been already recognized (like the one below), and turn it into a numpy array for future manipulations.

part of the sudoku:

Part of the sudoku

If you are wandering those rectangles are being drawn with 4 points collected by this part of my program:

contours, hierarchy = findContours( thresh.copy(), RETR_TREE, CHAIN_APPROX_SIMPLE)

for cnt in contours:
    rect = minAreaRect(cnt)
    if rect[1][0] > 80:
        box = boxPoints(rect)
        box = np.int0(box)
        if thresh[box[0][1], box[0][0]] != 0:
            for coord in box:
                coords.append(coord)
            approx = approxPolyDP(box,0.01*arcLength(box,True),True)
            drawContours(img,[approx],0,(255,0,0),2)

I didn't find any solution on the internet so I'm asking: is there any way I can do that?

Upvotes: 0

Views: 138

Answers (1)

Brian
Brian

Reputation: 56

You can use the contour bounding points to crop the image with slicing and store it in a new array:

# (x1, y1) is the top-left bounding point
# (x2, y2) is the bottom-right bounding point
sudoku_box = img[y1:y2, x1:x2]

Upvotes: 2

Related Questions