x,y,w,h = cv2.boundingRect(ctr)
leng = int(h*.9)
pt1 = int(y + h // 2 - leng // 2)
pt2 = int(x + w // 2 - leng // 2)
roi = im_th[pt1-int(leng*0.6) : pt1+ int(leng*1.2) , pt2: pt2+leng]
cv2.imwrite("C:\\Users\\kumar\\Desktop\\final year\\test_image\\"+str(idx)+".jpg",roi)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,0),3)
roi = roi/255
# Resize the image
roi = cv2.resize(roi, (28, 28), interpolation=cv2.INTER_AREA)
roi=roi*255
cv2.imwrite("C:\\Users\\kumar\\Desktop\\final year\\test_image\\after_resize_"+str(idx)+".jpg",roi)
cv2.waitKey(0)
使用此代码块从图像字母(例如m和u)中裁剪出的字母时,会产生错误。