0

这是主要的输入图像(名为1.png):

名为 1.png 的图片

现在,我想确定哪个盒子是用颜色填充的,哪个盒子是空的,在中心的大盒子里,而不是 20 个有数字的边盒子里。

我编写了从图像中提取主大框的代码:

image = cv2.imread(path)

gray = cv2.cvtColor(image , cv2.COLOR_BGR2GRAY)

edge = cv2.Canny(gray.copy() , 10 ,70)

_,contours,_ = cv2.findContours(edge.copy() , cv2.RETR_EXTERNAL ,cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(image, contours , -1, (0,0,255) , 3)
plt.imshow(image)
cv2.imwrite('image.jpg',image)

现在图像看起来像这样:

新的图像外观

然后我对轮廓进行排序以获得包含所有彩色和非彩色框的主矩阵框。

sorted_contours = sorted(contours,key = cv2.contourArea,reverse=True)

然后我分离了主箱:

img = cv2.imread('1.png')
cnt = sorted_contours[0]
x,y,w,h = cv2.boundingRect(cnt)
main_box = img[y:y+h,x:x+w].copy()
cv2.imwrite('main_box.jpg',main_box)
plt.imshow(main_box)

所以,主框现在看起来像这样:

image_box.jpg

标记外箱

main_box_gray = cv2.cvtColor(main_box,cv2.COLOR_BGR2GRAY)
_, main_box_gray = cv2.threshold(main_box_gray,5,255,cv2.THRESH_BINARY)


_,t_c,_ = cv2.findContours(main_box_gray.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

colored_main_box = img[y:y+h,x:x+w].copy()

cv2.drawContours(colored_main_box,t_c,-1,(255,0,0),2)

外框已标记,现在我将外轮廓分开以获取里面的框:

sorted_box = sorted(t_c,key = cv2.contourArea,reverse=True)

colored_main_box = img[y:y+h,x:x+w].copy()
cnt = sorted_box[0]

x2,y2,w2,h2 = cv2.boundingRect(cnt)

temp_image = colored_main_box[y2:y2+h2,x2:x2+w2].copy()


edge_temp = cv2.Canny(temp_image,100,200)

_,t_c_1,_ = cv2.findContours(edge_temp.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

colored_main_box = img[y:y+h,x:x+w].copy()

colored_main_box = colored_main_box[y2:y2+h2,x2:x2+w2].copy()

cv2.drawContours(colored_main_box,t_c_1,-1,(0,0,255),1)

plt.imshow(colored_main_box)
cv2.imwrite("full_marked.jpg",colored_main_box)

colored_main_box(满分):

coloured_main_box

现在我已经标记了所有内框,我可以使用 cv2.countNonZero 函数找到填充了哪个框,但我的主要任务是将填充框内的颜色保存为单独的图像,而没有包含该颜色的框的外部黑色边框填充和仅包含颜色填充的保存图像应以提取它的框命名。

例如:在main_box.jpg图像中:

image_box.jpg

橙色应另存为8.jpg.

红色应另存为12.jpg.

绿色应另存为18.jpg.

黄色应另存为19.jpg.

蓝色应该是 number 21.jpg

请帮助我使用代码来提取和保存带有正确数字标签的框内的颜色填充。

4

1 回答 1

0

您需要解决两个问题的子集。

  1. 获取彩色框的边界框。
  2. 使用框索引命名它们所以首先我们需要裁剪与每种颜色对应的图像。要获取 Image 中的颜色,请使用以下命令:
unique_col = set( tuple(v) for col in image for v in col )     
for color in unique_col:
    if col == (0,0,0) or col == (255,255,255):
       continue
    idx_set = np.where((image == col).all(axis=2))
    min_x = min(idx_set[:,0])
    min_y = min(idx_set[:,1])
    max_x = max(idx_set[:,0])
    max_y = max(idx_set[:,1]) 
    #this will give you bounding box for that particular color

第2步:分配正确的名称让bbox_small成为主大框中每个小框的边界框,然后根据所有矩形的一个角的y坐标(=sorted_bbox数组)对其进行排序。并将 key(id) 分配给每个框 = sorted_bbox 中该框的索引 + 1

现在

for every colored box:
    name = i+1 such that colored_box lies inside sorted_box[i]

我没有写任何代码,它只是一个想法。另外我认为使用霍夫变换来获取矩形的角也是一个好方法。

于 2018-12-13T16:55:19.010 回答