# cv2.waitKey(0) # img_copy = img.copy() # cv2.drawContours(img_copy, [box], -1, (255, 0, 0), 2) # cv2.imshow('box', img_copy) # # warp = four_point_transform(img, box) # cv2.imshow('warp image', warp) # cv2.waitKey(0) # our_cnt = None peri = cv2.arcLength(approx_cnt, True) approx = cv2.approxPolyDP(approx_cnt, 0.02 * peri, True) if len(approx) == 4: our_cnt = approx # warp = warp(our_cnt, img) cv2.imshow('warp image', warp) # cv2.waitKey(0) # shape, th_digits = detect(box, v) # digits = cv2.findContours(th_digits.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # digits = imutils.grab_contours(digits) # cv2.drawContours(warp, digits, -1, (255, 0, 0), 2) # cv2.imshow('digit', warp) # cv2.waitKey(0) # # digitcnts = [] # for cc in digits: # (x, y, w, h) = cv2.boundingRect(cc) # if (w >= 4 and w <= 40) and (h >= 25 and h <= 30): # digitcnts.append(cc)
def addressDetect(img, maskG): address = None maskG_cnt, _, maskG_approx = geom.find_main_contour_approx(maskG) W = 0 H = 0 if maskG_approx is not None and len(maskG_approx) == 4: maskG_cnt = max(maskG_cnt, key=cv2.contourArea) maskG_rect = cv2.minAreaRect(maskG_cnt) maskG_box = np.int0(cv2.boxPoints(maskG_rect)) W = int(maskG_rect[1][0]) H = int(maskG_rect[1][1]) maskG_size = W * H if maskG_size >= 1000 and maskG_size < 3000: peri = cv2.arcLength(maskG_approx, True) approx = cv2.approxPolyDP(maskG_approx, 0.02 * peri, True) warp_img = warp(approx, img) thresh = make_thresh(warp_img) thresh_cut = [] if thresh.shape[1] >= 45: for i in range(3): cut_size = thresh.shape[1] // 3 if i == 2: cut_img = thresh[:, i * cut_size:] else: cut_img = thresh[:, i * cut_size:(i + 1) * cut_size] thresh_cut.append(cut_img) digits = [] for i in range(3): cut_img = thresh_cut[i] each_cnts, max_cnt, box = geom.find_main_contour_box(cut_img.copy()) (x, y, w, h) = cv2.boundingRect(max_cnt) # (x, y, w, h) = box if i == 0: if w < 8: digits.append(1) else: digits.append(2) elif i == 1: digits.append(0) else: if w < 8 and h >= 10: digits.append(1) elif h >= 10: roi = cut_img[y:y + h, x:x + w] (roiH, roiW) = roi.shape # (dW, dH) = (int(roiW * 0.25), int(roiH * 0.15)) (dH, dW) = (int(roiH * 0.15), int(roiW * 0.2)) dHC = int(roiH * 0.1) segments = [ ((0, 0), (roiW, dH)), # top ((0, dH), (dW, roiH // 2)), # top-left ((roiW - dW, dH), (roiW, roiH // 2)), # top-right ((0, (roiH // 2) - dHC), (roiW, (roiH // 2) + dHC)), # center ((0, roiH // 2), (dW, roiH)), # bottom-left ((roiW - dW, roiH // 2), (roiW, roiH)), # bottom-right ((0, roiH - dH), (roiW, roiH)) # bottom ] on = [0] * len(segments) for (segi, ((xA, yA), (xB, yB))) in enumerate(segments): segROI = roi[yA:yB, xA:xB] total = cv2.countNonZero(segROI) area = (xB - xA) * (yB - yA) if total / float(area) > 0.55: on[segi] = 1 try: digit = DIGITS_LOOKUP[tuple(on)] digits.append(digit) except: pass if len(digits) == 3: address = '{}{}{}'.format(*digits[:3]) cv2.drawContours(img, [maskG_box], -1, (0, 255, 0), 2) cv2.putText(img, '{}{}{}'.format(*digits[:3]), (img.shape[1]-100, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) return img, address
maskG_cnt = max(maskG_cnt, key=cv2.contourArea) maskG_rect = cv2.minAreaRect(maskG_cnt) W = int(maskG_rect[1][0]) H = int(maskG_rect[1][1]) if cv2.countNonZero(maskG) >= 1000 and (W*H >= 1000) and (W*H < 4000): cntG, max_cntG, approx_cntG = geom.find_main_contour_approx(maskG) img_copy = img.copy() cv2.drawContours(img_copy, max_cntG, -1, (0, 255, 0), 2) cv2.imshow('Green contours', img_copy) # cv2.waitKey(0) peri = cv2.arcLength(approx_cntG, True) approx = cv2.approxPolyDP(approx_cntG, 0.02*peri, True) if len(approx) == 4 : our_cnt = approx warp_img = warp(our_cnt, img) cv2.imshow('warp', warp_img) # cv2.waitKey(0) warp_gray = cv2.cvtColor(warp_img, cv2.COLOR_BGR2GRAY) warp_gray = cv2.GaussianBlur(warp_gray, (3,3),0) thresh = cv2.threshold(warp_gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] # thresh = cv2.adaptiveThreshold(warp_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY|cv2.THRESH_OTSU, 3, 2)[1] th_kernel = np.ones((2,2), np.uint8) thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, th_kernel) thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, th_kernel) cv2.imshow('thresh', thresh) cv2.waitKey(0) thresh_cut = [] if thresh.shape[1] < 50 :