0

I am trying to crop 6 digit handwritten zip code for zip code sorting application,given code work fine with a 10 digit phone number but fails to recognize 6 digit contours. Please correct the given code or present better alternative code to accomplish the above mention task.

import numpy as np
import cv2
import imutils


image = cv2.imread("pin5.jpg")
image = imutils.resize(image, width=500)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)


ret,thresh1 = cv2.threshold(gray ,127,255,cv2.THRESH_BINARY_INV)


dilate = cv2.dilate(thresh1, None, iterations=2)


cnts = cv2.findContours(dilate.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] 
orig = image.copy()
i = 0

for cnt in cnts:

    if(cv2.contourArea(cnt) < 80):
        continue


    x,y,w,h = cv2.boundingRect(cnt)


    roi = image[y:y+h, x:x+w]


    cv2.rectangle(orig,(x,y),(x+w,y+h),(0,255,0),2)

    cv2.imwrite("roi" + str(i) + ".png", roi)

    i = i + 1

cv2.imshow("Image", orig)
cv2.waitKey(0)

0 Answers0