def Crop_pic(ps, img_path, cropimg_path, perspective_path, txt_file, i, trans_path, save_path1, save_path2):
    img = cv2.imread(img_path)
    perspective3 = np.float32([[0, 0], [383, 0], [383, 383], [0, 383]])
    perspective3_ = np.float32([[0, 0], [383, 0], [383, 383]])
    num = 0
    for line in ps:
        num = (num + 1)
        arr0 = random.randint(80, 120)
        arr1 = random.randint(80, 120)
        arr2 = random.randint(263, 303)
        arr3 = random.randint(80, 120)
        arr4 = random.randint(263, 303)
        arr5 = random.randint(263, 303)
        arr6 = random.randint(80, 120)
        arr7 = random.randint(263, 303)
        perspective0 = np.float32([[line[0], line[1]], [line[2], line[3]], [line[4], line[5]], [line[6], line[7]]])
        perspective0_ = np.float32([[line[0], line[1]], [line[2], line[3]], [line[4], line[5]]])
        colorize(perspective0, img, save_path1, i, num, (0, 255, 0))
        perspective1 = np.float32([[arr0, arr1], [arr2, arr3], [arr4, arr5], [arr6, arr7]])
        perspective1_ = np.float32([[arr0, arr1], [arr2, arr3], [arr4, arr5]])
        trans_inv = cv2.getAffineTransform(perspective1_, perspective0_)
        dst = []
        mat = np.array([[0, 0, 1], [383, 0, 1], [383, 383, 1], [0, 383, 1]], dtype=np.float32)
        mat = mat.transpose()
        dst = np.dot(trans_inv, mat)
        dst = dst.transpose()
        paint_line(img, dst, cropimg_path, num)
        perspective2_ = np.float32([[dst[0][0], dst[0][1]], [dst[1][0], dst[1][1]], [dst[2][0], dst[2][1]]])
        trans = cv2.getAffineTransform(perspective2_, perspective3_)
        dst2 = cv2.warpAffine(img, trans, (384, 384))
        mat2 = np.array([[line[0], line[1], 1], [line[2], line[3], 1], [line[4], line[5], 1], [line[6], line[7], 1]], dtype=np.float32)
        mat2 = mat2.transpose()
        point = np.dot(trans, mat2)
        point = point.transpose()
        perspective_path1 = os.path.join(perspective_path, (((i.strip('.jpg') + '_') + str(num)) + '.jpg'))
        cv2.imwrite(perspective_path1, dst2)
        colorize(point, dst2, save_path2, i, num, (0, 255, 0))
        txt_file1 = os.path.join(txt_file, (((i.strip('.jpg') + '_') + str(num)) + '_OA.txt'))
        with open(txt_file1, 'w') as f:
            for j in range(4):
                f.write(str(point[j][0]))
                f.write(' ')
                f.write(str(point[j][1]))
                f.write('\n')
        trans_path1 = os.path.join(trans_path, (((i.strip('.jpg') + '_') + str(num)) + '.txt'))
        with open(trans_path1, 'w') as ff:
            for j in range(2):
                for k in range(3):
                    ff.write(str(trans_inv[j][k]))
                    ff.write(' ')
def Crop_pic(ps, img_path, cropimg_path, perspective_path, txt_file, i, trans_path, save_path1, save_path2):
    img = cv2.imread(img_path)
    perspective3 = np.float32([[0, 0], [383, 0], [383, 383], [0, 383]])
    perspective3_ = np.float32([[0, 0], [383, 0], [383, 383]])
    num = 0
    for line in ps:
        num = (num + 1)
        arr0 = random.randint(80, 120)
        arr1 = random.randint(80, 120)
        arr2 = random.randint(263, 303)
        arr3 = random.randint(80, 120)
        arr4 = random.randint(263, 303)
        arr5 = random.randint(263, 303)
        arr6 = random.randint(80, 120)
        arr7 = random.randint(263, 303)
        perspective0 = np.float32([[line[0], line[1]], [line[2], line[3]], [line[4], line[5]], [line[6], line[7]]])
        perspective0_ = np.float32([[line[0], line[1]], [line[2], line[3]], [line[4], line[5]]])
        colorize(perspective0, img, save_path1, i, num, (0, 255, 0))
        perspective1 = np.float32([[arr0, arr1], [arr2, arr3], [arr4, arr5], [arr6, arr7]])
        perspective1_ = np.float32([[arr0, arr1], [arr2, arr3], [arr4, arr5]])
        trans_inv = cv2.getAffineTransform(perspective1_, perspective0_)
        dst = []
        mat = np.array([[0, 0, 1], [383, 0, 1], [383, 383, 1], [0, 383, 1]], dtype=np.float32)
        mat = mat.transpose()
        dst = np.dot(trans_inv, mat)
        dst = dst.transpose()
        paint_line(img, dst, cropimg_path, num)
        perspective2_ = np.float32([[dst[0][0], dst[0][1]], [dst[1][0], dst[1][1]], [dst[2][0], dst[2][1]]])
        trans = cv2.getAffineTransform(perspective2_, perspective3_)
        dst2 = cv2.warpAffine(img, trans, (384, 384))
        mat2 = np.array([[line[0], line[1], 1], [line[2], line[3], 1], [line[4], line[5], 1], [line[6], line[7], 1]], dtype=np.float32)
        mat2 = mat2.transpose()
        point = np.dot(trans, mat2)
        point = point.transpose()
        perspective_path1 = os.path.join(perspective_path, (((i.strip('.jpg') + '_') + str(num)) + '.jpg'))
        cv2.imwrite(perspective_path1, dst2)
        colorize(point, dst2, save_path2, i, num, (0, 255, 0))
        txt_file1 = os.path.join(txt_file, (((i.strip('.jpg') + '_') + str(num)) + '_OA.txt'))
        with open(txt_file1, 'w') as f:
            for j in range(4):
                f.write(str(point[j][0]))
                f.write(' ')
                f.write(str(point[j][1]))
                f.write('\n')
        trans_path1 = os.path.join(trans_path, (((i.strip('.jpg') + '_') + str(num)) + '.txt'))
        with open(trans_path1, 'w') as ff:
            for j in range(2):
                for k in range(3):
                    ff.write(str(trans_inv[j][k]))
                    ff.write(' ')