def xihua(): for lists in os.listdir(Constants.IMG_DIR_TENCENT_TRAIN): #print(lists) path = os.path.join(Constants.IMG_DIR_TENCENT_TRAIN, lists) if not os.path.isdir(path): print(path) oriImg = cv2.imread(path, 0) img = Util.binaryzation(oriImg) img = Util.erasePaddingInOneChar(img) #cv2.imshow("ori", img) #height, width = img.shape img = ~Util.skeleton(~img) cv2.imwrite(path, img) #ret.append(readOneImg(path)) pass
def readOneImg(path): oriImg = cv2.imread(path, 0) img = Util.binaryzation(oriImg) img = Util.erasePaddingInOneChar(img) height, width = img.shape #获取指定的三个分割点, 再根据分割点做分割线 points, directions = getSpiltPoint(path[path.rindex('/'):]) path1 = Util.fillPath(SpiltUtil.spiltOne(img, points[0], directions[0], 0), height) path2 = Util.fillPath(SpiltUtil.spiltOne(img, points[1], directions[1], 1), height) path3 = Util.fillPath(SpiltUtil.spiltOne(img, points[2], directions[2], 2), height) path0 = Util.getLeftBorder(height) path4 = Util.getRightBoder(width, height) ret = [] ret.extend(Util.conventToTrainCharFromAllWrap(getOneChar(img, path0, path1))) ret.extend(Util.conventToTrainCharFromAllWrap(getOneChar(img, path1, path2))) ret.extend(Util.conventToTrainCharFromAllWrap(getOneChar(img, path2, path3))) ret.extend(Util.conventToTrainCharFromAllWrap(getOneChar(img, path3, path4))) return ret
def readInput(svm, puzzle): oriImg = cv2.imread(puzzle, 0) img = Util.binaryzation(oriImg) img = Util.erasePaddingInOneChar(img) points, paths = SpiltUseBackground.getCornerPointsAndPaths(img) print("-------RECOGNISE--------") print(points) print("paths:") for p in paths: print(p) height, width = img.shape code = [] #p1 = [23:39] path0 = Util.getLeftBorder(height) good_path, result = recogniseOne(svm, img, path0, paths, 0) print("First split point:" + str(good_path[0][0]) + " result:" + chr(svm.classes_[result] + ord('A'))) code.append(chr(svm.classes_[result] + ord('A'))) #path0 = Util.fillPath(SpiltUseCV2.spiltOne(img, p1), height) good_path, result = recogniseOne(svm, img, good_path, paths, 1) print("Second split point:" + str(good_path[0][0]) + " result:" + chr(svm.classes_[result] + ord('A'))) code.append(chr(svm.classes_[result] + ord('A'))) #path0 = Util.fillPath(SpiltUseCV2.spiltOne(img, p2), height) good_path, result = recogniseOne(svm, img, good_path, paths, 2) print("Third split point:" + str(good_path[0][0]) + " result:" + chr(svm.classes_[result] + ord('A'))) code.append(chr(svm.classes_[result] + ord('A'))) #path0 = Util.fillPath(SpiltUseCV2.spiltOne(img, p3), height) good_path, result = recogniseOne(svm, img, good_path, paths, 3) print("Fourth split point:" + str(good_path[0][0]) + " result:" + chr(svm.classes_[result] + ord('A'))) code.append(chr(svm.classes_[result] + ord('A'))) print("Result:" + str(code)) return code
def spiltOne(img, startX, direction, charIndex): #for j in range(startX, endX): height, width = img.shape path = [] img = Util.erasePaddingInOneChar(img) img = ~Util.skeleton(~img) #Util.printCV2Image(img) #cv2.imshow("1", img) if direction == Constants.SEARCH_FROM_TOP: print("direction: t") path.append((startX, 0)) searchOnceFromTop(img, path, startX, 0, charIndex) else: print("direction: b") path.append((startX, height - 1)) searchOnceFromBottom(img, path, startX, height - 1, charIndex) #print(path) #for i in range(len(path.path) - 1): # cv2.line(img, path.paths[i].toTuple(), path.paths[i - 1].toTuple(), 0) #cv2.imshow("Image", img) #if we want show a img, must add this #cv2.waitKey (0) return path
def erasePaddingInAllChar(target): for img in target: for char in img: char = Util.erasePaddingInOneChar(char)
#膨胀图像 def dilate(img): #OpenCV定义的结构元素 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) #膨胀图像 dilated = cv2.dilate(img, kernel) return dilated if __name__ == "__main__": #xihua() oriImg = cv2.imread( Constants.IMG_DIR_TENCENT_TRAIN + "AANV_39b_58b_82b.jpg", 0) img = Util.binaryzation(oriImg) #img = dilate(~img) img = Util.erasePaddingInOneChar(img) #img = Util.binaryzation(oriImg) #img = ~Util.skeleton(~img) cv2.imwrite(Constants.IMG_DIR_TENCENT_TRAIN + "1.jpg", img) '''img = cv2.imread(Constants.IMG_DIR_TENCENT_PUZZLE + "mbha.jpg") im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) im = cv2.threshold(im, 0, 255, cv2.THRESH_OTSU)[1] im[im == 255] = 1 im = morphology.skeletonize(im) im = im.astype(np.uint8) im[im == 1] = 255 cv2.imshow("Image1", im) cv2.imshow("Image2", img) im[im == 255] = 1