这是主要的输入图像(名为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)
所以,主框现在看起来像这样:

标记外箱
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(满分):

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

橙色应另存为8.jpg.
红色应另存为12.jpg.
绿色应另存为18.jpg.
黄色应另存为19.jpg.
蓝色应该是 number 21.jpg。
请帮助我使用代码来提取和保存带有正确数字标签的框内的颜色填充。