def wrapAffine(): img = cv2.imread(Constants.IMG_DIR_TENCENT_TEST + "H.jpg", 0) img = Util.binaryzation(img) rows, cols = img.shape pts1 = np.float32([[0, 0], [0, 20], [20, 0]]) #这样变换会横向变窄 #pts2 = np.float32([[0,0],[0,20],[18,0]]) #这样会纵向变扁 #pts2 = np.float32([[0,0],[0,18],[20,0]]) #这样横向变窄,且有一定的变形 #pts2 = np.float32([[0,0],[0,20],[18,2]]) #这样纵向变窄,且有一定的变形 pts2 = np.float32([[0, 0], [2, 18], [20, 0]]) pts2All = [ np.float32([[0, 0], [0, 20], [18, 0]]), np.float32([[0, 0], [0, 18], [20, 0]]), np.float32([[0, 0], [0, 20], [18, 2]]), np.float32([[0, 0], [2, 18], [20, 0]]) ] #pts2 = np.float32([[0,0],[20,20],[20,0]]) M = cv2.getAffineTransform(pts1, pts2All[0]) print(M) dst = cv2.warpAffine(~img, M, (cols, rows)) dst = Util.binaryzation(dst) cv2.imshow("2", img) cv2.imshow("1", ~dst) Util.printCV2Image(img) Util.printCV2Image(~dst) cv2.waitKey(0)
def convertToBinary(): dir_path = Constants.IMG_DIR_TENCENT_TRAIN + "other/" for lists in os.listdir(dir_path): #print(lists) path = os.path.join(dir_path, lists) if not os.path.isdir(path): oriImg = cv2.imread(path, cv2.CV_LOAD_IMAGE_GRAYSCALE) img = Util.binaryzation(oriImg) cv2.imwrite(path, img)
def testRotate(): oriImg = cv2.imread(Constants.IMG_DIR_TENCENT_PUZZLE + "mbha.jpg", 0) img = Util.binaryzation(oriImg) img = img[:, 0:42] rows, cols = img.shape M = cv2.getRotationMatrix2D((cols / 2, rows / 2), -10, 1) print(M) dst = cv2.warpAffine(img, M, (cols, rows)) cv2.imshow('img', dst) cv2.waitKey(0)
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 testAllPoints(): ret = [] labels = [] dir_path = Constants.IMG_DIR_TENCENT_TRAIN for lists in os.listdir(dir_path): #print(lists) path = os.path.join(dir_path, lists) if not os.path.isdir(path): oriImg = cv2.imread(path, 0) img = Util.binaryzation(oriImg) points, paths = getCornerPointsAndPaths(img) print("----------------RESULT----------------") print(path) print(points) print(len(paths)) #target = readOneImg(dir_path + "YbUB_39_68_85.jpg") return ret, labels
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
在这里进行wrap变换应该是最佳的 ''' target = Util.copyOneImg(target, (height, width)) return Util.wrap(target) #膨胀图像 def dilate(img, element_width): #OpenCV定义的结构元素 kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(element_width, element_width)) #膨胀图像 dilated = cv2.dilate(~img, kernel) return ~dilated if __name__ == "__main__": fileName = Constants.IMG_DIR_TENCENT_TRAIN + "BSmP_39_54_81.jpg" readOneImg(fileName) oriImg = cv2.imread(fileName, 0) img = Util.binaryzation(oriImg) #img = Util.erasePaddingInOneChar(img) img = dilate(img, 7) img = ~Util.skeleton(img) cv2.imwrite(Constants.IMG_DIR_TENCENT_TRAIN + "ske2.jpg", img) #cv2.imshow("ske1",img) '''path = Constants.IMG_DIR_TENCENT_TRAIN + "VmnZ_31_59_84.jpg" points, directions = getSpiltPoint(path[path.rindex('/'):]) print(points) print(directions) print(directions[0] == Constants.SEARCH_FROM_TOP)''' cv2.waitKey(0)