简体   繁体   English

图片的一部分变成numpy数组

[英]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. 我想拍摄图像的一部分,比方说已经被识别的数独的一部分(如下面的部分),然后将其转换为一个numpy数组,以备将来使用。

part of the sudoku : 数独的一部分

数独的一部分

If you are wandering those rectangles are being drawn with 4 points collected by this part of my program: 如果您正在徘徊,那么这些矩形将被我的程序的这一部分收集的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)

I didn't find any solution on the internet so I'm asking: is there any way I can do that? 我在互联网上找不到任何解决方案,所以我问:有什么办法可以做到?

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM