将图像的一部分放入 numpy 数组

Part of an image into numpy array

我正在做一个关于实时数独识别的项目,我 运行 遇到了一个问题。我想获取图像的一部分,比方说已经被识别的数独游戏的一部分(如下所示),并将其转换为一个 numpy 数组以供将来操作。

部分数独:

如果你在漫游,这些矩形是用我程序的这一部分收集的 4 个点绘制的:

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)

我没有在互联网上找到任何解决方案,所以我想问:有什么办法可以做到吗?

您可以使用轮廓边界点对图像进行切片裁剪并将其存储在新数组中:

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