轮廓化和包围图像中的单独字母,并将每个字母另存为单独的图像

时间:2019-05-07 01:00:56

标签: image opencv image-processing python-tesseract

我可以使用Pytesseract从图像中提取字母并将其另存为.png图像文件吗?如果没有,我该怎么做?我想在图像中找到单独的字母,将它们绑定,然后将它们另存为单独的图像。目前,我正在使用此代码进行尝试,但是它一直给我odd results。像线条和非常小的像素图像。我从中提取字母的图像是400 x 400像素。

    for i in range(IMAGES):
            imagePath = user + "/" + str(i+1) + ".png"

            image = cv2.imread(imagePath)
            im_bw = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
            ret,thresh1 = cv2.threshold(im_bw,127,255,cv2.THRESH_BINARY)
            contours, hierarchy = cv2.findContours(im_bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            for cnt in contours:
                    x,y,w,h = cv2.boundingRect(cnt)
                    cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),3)

            i=0
            for cnt in contours:
                    x,y,w,h = cv2.boundingRect(cnt)
                    letterImage = cv2.imwrite(user+"/letter"+str(i)+".png",thresh1[y:y+h,x:x+w])
                    letterImage = cv2.resize(letterImage, (28, 28))
                    i += 1

0 个答案:

没有答案