#  -*- coding: utf-8 -*-
import cv2
import os


def Edge_Extract(root, root2, root3):
    img_root = os.path.join(root)			# 修改为保存图像的文件名
    edge_root = os.path.join(root2)			#
    binary_root = os.path.join(root3)

    if not os.path.exists(edge_root):
        os.mkdir(edge_root)

    if not os.path.exists(binary_root):
        os.mkdir(binary_root)

    file_names = os.listdir(img_root)
    img_name = []

    for name in file_names:
        if not name.endswith('.jpg'):
            assert "This file %s is not JPG" % (name)
        img_name.append(os.path.join(img_root, name[:-4]+'.jpg'))

    index = 0
    for image in img_name:
        img = cv2.imread(image, 0)
        ret, img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
        cv2.imwrite(binary_root + '/' + file_names[index], img)
        cv2.imwrite(edge_root + '/' + file_names[index], cv2.Canny(img, 100, 300))
        index += 1
    return 0


if __name__ == '__main__':
    root = 'D:/result_contract/Otherkind/Tongue/20191119/86_extraction_SRG/' 	# 修改为你对应的文件路径
    root2 = 'D:/result_contract/Otherkind/Tongue/20191119/86_SRG_edge/'
    root3 = 'D:/result_contract/Otherkind/Tongue/20191119/86_SRG_binary/'
    Edge_Extract(root, root2, root3)
