import cv2
import numpy as np
img = cv2.imread('1.png',0)
image, contours, hierarchy = cv2.findContours(img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
boxes = list()
cropped_imageList = list()
for c in contours:
#if you want to store the original coordinates
x,y,w,h = cv2.boundingRect(c)
boxes.append((x,y,w,h))
#crop out the part of the image and resize
crop_img = img[y:y+h, x:x+w]
crop_img = cv2.resize(crop_img,(50,50))
cropped_imageList.append(crop_img)
#join them back
imstack = cropped_imageList[0]
for imgs in cropped_imageList[1:]:
imstack = np.hstack((imstack,imgs))
1 回答
这很简单 . 找到轮廓,遍历轮廓,找到每个轮廓的边界矩形,裁剪零件,调整裁剪零件的大小并将它们存储在列表中 . 然后,您可以使用numpy的
hstack
迭代新列表并加入它们 . 我已经分享了完整的代码,但是你需要一些过滤技术来删除多余的轮廓,所以要注意这一点 .码:
注意:这将随机加入找到的字母,解决此问题的一种方法是使用
x,y
对boxes
列表进行排序,然后进行裁剪 .当tesseract可以直接检测文本时,你为什么要这样做呢?如果你想要更好的结果,你应该使用深度学习来接近现代 .