result.append(box_left)
result.append(box_right)
else:
box = np.int0([[x,y], [x+w,y], [x+w,y+h], [x,y+h]])
result.append(box)
分割后,图片变成了这样
![在这里插入图片描述](https://img-blog.csdnimg.cn/57adfd410d924e508a5e5abee87e4cb3.png)
**4个字符被识别成2个字符**
4个字符被识别成2个字符有下面两种情况
![在这里插入图片描述](https://img-blog.csdnimg.cn/992545e16e5b42a1a6c975fc08e144c2.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/6c133986cb6b43718d65ae1bbe001697.png)
对第一种情况,对于左右两个轮廓,从中间分割即可。对第二种情况,将包含了3个字符的轮廓在水平方向上三等分。具体代码如下
result = []
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if w == w_max and w_max >= w_min * 2:
# 如果两个轮廓一个是另一个的宽度的2倍以上,我们认为这个轮廓就是包含3个字符的轮廓
box_left = np.int0([[x,y], [x+w/3,y], [x+w/3,y+h], [x,y+h]])
box_mid = np.int0([[x+w/3,y], [x+w*2/3,y], [x+w*2/3,y+h], [x+w/3,y+h]])
box_right = np.int0([[x+w*2/3,y], [x+w,y], [x+w,y+h], [x+w*2/3,y+h]])
result.append(box_left)
result.append(box_mid)
result.append(box_right)
elif w_max < w_min * 2:
# 如果两个轮廓,较宽的宽度小于较窄的2倍,我们认为这是两个包含2个字符的轮廓
box_left = np.int0([[x,y], [x+w/2,y], [x+w/2,y+h], [x,y+h]])
box_right = np.int0([[x+w/2,y], [x+w,y], [x+w,y+h], [x+w/2,y+h]])
result.append(box_left)
result.append(box_right)
else:
box = np.int0([[x,y], [x+w,y], [x+w,y+h], [x,y+h]])
result.append(box)
分割后的图片如下
![在这里插入图片描述](https://img-blog.csdnimg.cn/0e4a670a8b7d4fc5912a965c5b8f6234.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/522f57b317924966982473c1f8003506.png)
**4个字符被识别成1个字符**
![在这里插入图片描述](https://img-blog.csdnimg.cn/101e85217003461fad07bd2fb6d1d916.png)
这种情况对轮廓在水平方向上做4等分即可,代码如